Classes Tutorial 2024
Classes Tutorial 2024
Tutorial
Working with Classes
2
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
Getting Started
4 CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
The Add New Item Window
5 CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
The New Student Class
❑Creating a new
empty Class will
Adds a new class file
(.vb) to project
6 CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
Create Member Variables
❑A variable declared inside a class declaration
❑Syntax:
AccessSpecifer VariableName As DataType
❑AccessSpecifier may be Public or Private
Member Variables
8 CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
Designing the User Interface
❑To use the class, you
we will create a GUI that
look as follows:
❑Add 4 labels, 4
textboxes and 3 buttons
and name them
appropriately.
❑My textboxes named:
txtID, txtFName,
txtLName,
txtAverageScore
❑My Buttons named:
btnRun, btnClear, btnExit
9
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
Creating an Instance of a Class
❑To create an instance of the student class, we will
use the click event of the “Run” Button.
❑OR
Dim freshman As New Student()
10
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
Creating an Instance of a Class
❑The freshman
variable is created
at the module level
(Global Variable)
11
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
Accessing Members
❑Can work with Public member variables of a
class object in code
❑Can store values in member variables with the
following:
freshman.strFirstName = “Kwaku"
freshman.strLastName = “Manu"
freshman.strId = “B02091156"
13
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
Test the Code
❑Run the code by
Clicking on the run
Button on the
toolbar
❑Change the
PUBLIC infront of
each Member
Variable to Private
❑Create a Property
Procedure for each
Property.
16
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
Create a Property Procedure (3/3)
❑Create another property for the student class, that will store
the average mark of a student (usually between 0 and 100):
Public Class Student
' Member variable
Private sngTestAvg As Single
Public Property TestAverage() As Single
Get
Return sngTestAvg
End Get
Set(ByVal value As Single)
If value >= 0.0 And value <= 100.0 Then
sngTestAvg = value
Else
MessageBox.Show("Invalid Value.", "Error")
End If
End Set
End Property
End Class
17
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
Create a Property Procedure (1/3)
❑Insert the rest of
the Properties
below the first
property but before
the END CLASS
statement as
follows:
18
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
Create a Property Procedure (2/3)
❑Insert the rest of the Properties below the first property but
before the END CLASS statement as follows:
‘Insert the rest of the Properties Here
Public Property FirstName() As String
Get
Return strFirstName
End Get
Set(ByVal value As String)
strFirstName = value
End Set
End Property
Public Property IDNumber() As String
Get
Return strId
End Get
Set(ByVal value As String)
strId = value
End Set
19 End Property
End Class CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
Create a Property Procedure (1/3)
❑Create another
property for the
student class, that
will store the
average mark of a
student (usually
between 0 and
100):
20
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
Create a another Property
' TestGrade property procedure
ReadOnly Property TestGrade() As Char
Get
If sngTestAvg >= 70 Then
return "A“
Else If sngTestAvg >= 60 Then
return "B“
Else If sngTestAvg >= 50 Then
return "C“
Else If sngTestAvg >= 40 Then
return "D“
Else
return "F“
End If
End Get
21
End Property
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
Create a Property Procedure (1/3)
❑Create another
property for the
student class, that
will USE the stored
average mark of a
student to calculate
the GRADE for the
Student
❑Using GTUC
standard for
grading(A - F):
22
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
Setting and Validating a Property
❑In the Click Event of the RUN button on form1 class, the
created properties are set as shown:
freshman.IDNumber = CStr(txtID.Text)
freshman.LastName = CStr(txtLName.Text)
freshman.FirstName = CStr(txtFName.Text)
freshman.TestAverage = CSng(txtAvrScore.Text)
❑After setting a value for the TestAverage property, the
TestGrade property can be accessed using the following:
txtGrade.Text = “Grade for “ &
freshman.IDNumber & “ ” &
freshman.FirstName & “ ” &
freshman.LastName &
“ With an average score of ” &
freshman.TestAverage & “ is: “ &
freshman.TestGrade
23
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
Setting and Validating a Property
❑Open the Code editor
for the form1 class
24
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
Test the Code
❑Run the code by
Clicking on the run
Button on the
toolbar or F5
❑Fill in the
appropriate data in
the Textboxes and
click on the Run
button to show the
information
25
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
Create a Method for Student Class
❑ Create the following sub procedure in the student class that will clear
the values stored if the properties
' Clear method
Public Sub Clear()
strFirstName = String.Empty
strLastName = String.Empty
strId = String.Empty
sngTestAvg = 0.0
End Sub
❑Call the Clear() Method in Click event of the CLEAR Button in the
form1 class using the following statement:
Freshman.Clear()
26
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
Create a Method for Student Class
❑Open the Code for
the Student Class
27
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
Using the Method
❑Open the form1
❑Double-click on
the Clear Button to
lunch thee code
editor
28
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
Test the Code
❑Run the code by Clicking on
the run Button on the toolbar or
F5
❑The form will appears
❑Fill in the appropriate data in
the Textboxes and click on the
Run button to show the
information
❑Click on the OK button to
close the dialog box.
❑Next click on the Clear button
to show the dialog box again
❑Click OK to close the first
dialog box and open the
second.
❑The second dialog box will
have no more values because
they have been cleared.
29
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
Create a Constructor
' Constructor
Public Sub New()
strFirstName = "(unknown)"
strLastName = "(unknown)"
strId = "(unknown)"
sngTestAvg = 0.0
End Sub
30
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
Create a Constructor
❑Open the Code for
the Student Class
31
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
Test the Code
❑Run the code by Clicking on the
run Button on the toolbar or F5
32
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
Creating Inherited Class
33 CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
Creating Inherited Class
34 CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
Instantiate the Inherited Class
❑Open the Code
editor for the form1
class
❑You can
instantiate and use
the new class by
writing the following
code in the click
event of the Run
button:
❑You can test the
code to see the
results
35
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
Creating Inherited Form Class
36 CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
The Add New Item Window
❑From the Add New Item
Window do the following:
❑From the
Inheritance Picker
Window, Select
Form1 and on OK to
create the new from
The Class
38 CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
Creating Inherited Form Class
39 CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
Individual Assignment
❑ Each student is to choose one of the classes in this list and
repeat all the exercises from slide 4 to slide 39 (create class,
properties, methods, constructor, inheritance for class and
interface). Run and take screen shots of each exercise and
upload a word document containing all the results on the site.
Class Attributes (properties) Operations (methods)