0% found this document useful (0 votes)
6 views

Classes Tutorial 2024

Uploaded by

Navas Click
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Classes Tutorial 2024

Uploaded by

Navas Click
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

CICS 314 : Advanced Visual

Basic .NET Programming

Tutorial
Working with Classes

Ghana Technology University College


Lecturer – Dr. Forgor Lempogo
2024
Objectives

❑By the end of this Lesson, students should be able to use


Visual Studio IDE to:
❑Create a class that has properties and methods.
❑Assign values to the properties with a constructor.
❑Instantiate an object in a project using your class.
❑Create and use constructors and destructors.
❑Use visual inheritance by deriving a form from another form.

2
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
Getting Started

❑Create a new windows forms application


❑Design a GUI for the project
❑Create a new class
❑Add member variables to the class
❑Add Properties to the class
❑Add methods to the class
❑Instantiate and use the class
3
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
Create the Class

❑To create a new class:


❑Select PROJECT from the
Menu
❑From the Dropdown Select
Add Class….
❑This will lunch the Add New
Item Window
❑OR
❑Click Add New Item button
on toolbar
❑Select Class from Add New
Item dialog box

4 CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
The Add New Item Window

❑From the Add New Item


Window, Select Class from
Add New Item dialog box
❑Provide a name for the
class – (mine is Student)
❑Click Add to create the
new Class

❑This will Adds a new class


file (.vb) to project

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

❑The new class file The Class


Declaration
will contain the class
The Class
declaration. file (.vb)

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

❑For the sake of the example, we will declare them


here as public for the mean time.
❑The following Example create 3 member variables:
Public Class Student
Public strLastName As String ‘Holds last name
Public strFirstName As String ‘Holds first name
Public strId As String ‘Holds ID number
End Class
7
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
Create Member Variables

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.

❑In the Click Event of the “Run” Button type the


following: Dim freshman As Student
freshman = New Student()

❑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)

❑At can now be


instantiated in the
click event of all the
buttons

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"

❑Can retrieve values from member variables


with the following (All in one line):
MessageBox.Show(“Hello” &
freshman.strFirstName & “ ” &
freshman.strLastName & “, your new ID is “ &
freshman.strid)
12 CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
Accessing Members
❑The freshman
variable is created
at the module level
(Global Variable)

❑At can now be


instantiated in the
click event of all the
buttons

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

❑When the form


appears click on the
Run button to show
the information

❑No need to enter


data into in to the
textboxes yet
14
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
Create a Property Procedure (1/3)
❑Create a property for the student class, by creating the
following procedure:
Public Class Student
' Member variables
Private strLastName As String ‘Holds last name
Private strFirstName As String ‘Holds first name
Private strId As String ‘Holds ID number

Public Property LastName() As String


Get
Return strLastName
End Get
Set(ByVal value As String)
strLastName = value
End Set
End Property
‘Insert the rest of the Properties Here
15
End Class
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
Create a Property Procedure (1/3)
❑Open the Code for
the Student Class

❑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

❑At this point there will


be a fair number of
Errors

❑This is because the


public Member
Variables in the student
class were changed to
Private.

❑To resolve these


Errors you need to
Rewrite/Change the
code in the click even of
the Run button to the
following:

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

❑The form will


appears

❑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

❑Insert the Method


code below the Last
property but before
the END CLASS
statement as
follows:

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

❑In the click event


to the Clear button
write the following
code:

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

❑Create a constructor in the student class that will set initial


values for the properties:

' 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

❑Insert the Method


code below the Last
property but before
the END CLASS
statement as
follows:

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

❑The form will appears


❑DO NOT Fill any data in the
Textboxes
❑Clicking on the Clear button will
show the dialog box with all values
being (Unknown)

❑That is the values we specified in


the Constructor

❑Click on the OK button to close the


dialog box.
❑Next Fill the textboxes with data
and click on the RUN button

❑Next Click on the Clear button


again

❑This time it will show the data


entered.

32
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
Creating Inherited Class

❑Create a new class


and give it an
appropriate name.
(mine is
BusinessStudent)

❑Creating a new The Class


empty Class will Adds Declaration
The Class
a new class file (.vb) to
file (.vb)
project

❑The new class file will


contain the class
declaration.

33 CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
Creating Inherited Class

❑To inherit the properties


and methods of the
Student class:

❑The following statement


must be written in the The parent
class declaration class

❑It must be the first The name of


the solution
statement in the class
declaration
❑After this the new class Key word
(BusinessStudent) will
have all the properties
and methods of Parent
class (Student)

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

❑To create a new class:


❑In the Project menu,
Add Inherited Form.
❑In dialog, select name
of base form.
OR
❑ In the Project menu,
Add Windows Form.

❑Modify the Inherits


Statement to inherit
from base form using
project name as the
namespace.

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 Installed Section


Select “Windows Forms”

❑Select Inherited Form


from the New Item Section

❑Provide a name for the


Form – (mine is
frmClassEx)

❑Click Add to lunch the


Inheritance Picker window
37 CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
Creating Inherited Form Class

❑From the
Inheritance Picker
Window, Select
Form1 and on OK to
create the new from
The Class

❑The new form will


Declaration
The Class
have all the buttons, file (.vb)
labels and
textboxes as well as
the design of form1

38 CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
Creating Inherited Form Class

❑The new form will


inherit all the
buttons, labels and
textboxes as well as The parent
the design of form1 class
The name of

❑You can add more


the solution

controls, but the old


controls cannot be Key word
moved

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)

StudentList AllStudents, Count Add, Remove,


FindStudent

Course Semester, Name, Display, Input


Grade,Credits

Transcript CourseList, Count Display, Search,


40
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2024 Delivery
CalcGradeAvg

You might also like