Download presentation
Presentation is loading. Please wait.
1
CSI 101 Elements of Computing Spring 2009 Lecture # 14 – Classes and Objects Wednesday, April 15th, 2009 and Monday, April 20th, 2009
2
What is a class? ● Traditionally, the term “class” has been used to describe a characteristic or category of data ● With the advent of Object-Oriented languages like C++, “class” has come to mean an object that is created by a programmer
3
What is an object? ● An object is a discrete collection of data, properties, and interface functions that together define and perform the services for a particular “item” ● For instance, the text box on a Form is an object. Its data is its value; it has properties and event procedures
4
Object-Oriented Programming ● Using objects as the primary focus of the program ● Shifts the focus of development from functions and procedures to data ● Intended to produce efficient programs ● Focus is transforming input to output using needed system data ● Eliminate extraneous functions
5
Principles of objects ● Data hiding ● Data is “internal” and Private to object ● Functions and procedures other than those included in object cannot touch the data ● All access to data is controlled by the procedures included in the object ● Called “methods”
6
More principles of objects ● Inheritance ● You can create a new class based upon an existing one ● Automatically gains data and methods of existing one ● Polymorphism ● Multiple methods with same name ● Must have at least one argument of a different type
7
Classes in Visual Basic ● Technically, Visual Basic doesn't have them, other than predefined Windows objects ● Visual Basic 2008 is based upon C++, which does have classes ● Class is defined with CLASS and END CLASS statements ● Example: Class Student End Class
8
Scope ● Public ● Default for classes ● Means any procedure in application can declare variables of that type ● Private ● Must be specified before CLASS keyword ● Private Class CIA ● Only available to local procedure ● Prevents reuse – not recommended
9
Creating a class in Visual Studio ● Click on Project on the menu bar ● Click on Add Class... in the dropdown menu ● Specify name of class in Name: field ● By default, uses Classn, where n is the number of the next class ● For instance, Class1 ● Click Add button
10
Member data ● These are the data types contained within the class ● Defined just as fields are in user-defined structures Class Student Fname as String Lname as String ID as Long End Class
11
Member data scope ● By default, all member data is Private ● Only methods defined within the class can access data ● Visual Basic does allow you to define Public member data, but it is not recommended ● Abolishes the Data Hiding principle of Object- Oriented Programming
12
Declaring a class variable ● Variables of class type are also called “instances” ● Add keyword NEW to DIM to create an instance ● DIM Ualb_newbie As New Student
13
Accessing member data ● Use the same dot notation that was used for user-defined structures and Form object properties ● Example: Ualb_newbie.Lname ● Class properties and methods are accessed the same way
14
Class Property ● Defined as a procedure ● Uses PROPERTY and END PROPERTY Property Name() As type End Property ● Type is the data type of the property ● Public is default scope ● Best to use that, if you anticipate needing to change a property
15
Property parts ● GET ● Code that executes to retrieve value ● Uses RETURN keyword ● SET ● Code used to set value ● Both have END statements ● End Get ● End Set
16
Property Example Class Student Public Property Name() As String Get Return Fname & Lname 'Note dot notation not required inside class End Get Set(ByVal inName As String) 'remember what ByVal means? Dim spaceLoc As Integer spaceLoc = InStr(inName,” “) 'find space Fname = Left(inName,spaceLoc) Lname = Mid(inName,spaceLoc+1) 'why spaceLoc + 1? End Set End Property
17
Using Class Properties Dim Ualb As Student Dim name as String 'get Name name = Ualb.Name 'set name Ualb.Name = name ● Note it operates just like Form object Properties
18
Readonly Properties ● Can only be read ● Has no Set process ● Uses Readonly keyword on Property statement ● Example: Public ReadOnly Property ID() As Long Get Return ID End Get End Property
19
Setting ReadOnly Properties ● Done when variable is declared ● Uses method called “constructor” ● Another steal from C++ ● Method with procedure name New() ● We'll see Methods on the next slide
20
Class Methods ● Used by language to allow other procedures to interact with Private (internal) items in class ● Either Sub or Function ● Contained within Class block ● Public is default scope. Keep that, or the procedure can’t be run
21
Method Example ● Within class Student: Public Function CanGraduate() As Boolean If ClassYear = Year(Now()) And GPA >= 2.0 Then CanGraduate = True ' Remember variable name same as function Else CanGraduate = False EndIf End Function
22
Let's create Student Class Data holds name, ID, Class Year, GPA, and total number of course hours Information on specific courses would be held elsewhere Properties: Name, ID, ClassYear, GPA, and Hours ID set at creation There is a predefined function to obtain a unique ID
23
Student Class Methods ● Add hours to existing total ● This is actually done as part of recalculating GPA ● Calculate new GPA ● Receive hours and GPA ● Calculate new average ● Graduation eligible? ● We’ve already seen that one in the previous slide
24
Student Class – Member data Public Class Student ‘Member data section Private Fname as String Private Lname as String Private ClassYear as Integer Private ID as Long Private GPA As Decimal Private ClassHours as Integer
25
Student Class – Name Property Public Property Name() Get Return Fname & “ “ & Lname End Get Set (ByVal inFirst as String, ByVal inLast as String) Fname = inFirst Lname = inLast End Set End Property
26
Student Class – ID Property Public ReadOnly Property ID() Get Return ID End Get End Property
27
Student Class – ClassYear Property Public ReadOnly Property ClassYear() Get Return ClassYear End Get ‘No Set – method to increment if held back End Property
28
Student Class - Properties ● Others are ReadOnly as well Public ReadOnly Property GPA() Get Return GPA End Get End Property Public ReadOnly Property Hours() Get Return ClassHours End Get End Property
29
Student Class - Constructor Public Sub New() ID = GetNewStudentID() ClassYear = Year(Now()) + 3 GPA = 0.0 ClassHours = 0 End Sub
30
Student Class – Calculate GPA Public Function ReCalcGPA(ByVal Hrs As Integer, ByVal inGPA As Decimal) As Decimal ‘Return new GPA Dim Sum As Integer = GPA * ClassHours Sum += inGPA * Hrs ClassHours += Hrs GPA = Sum / ClassHours Return GPA End Function
31
Student Class – Hold Back Method Public Sub HoldBack() ‘no arguments ClassYear += 1 End Sub End Class
32
Let's use Student Dim newStudent As New Student 'This causes New() constructor to run Dim myID = newStudent.ID 'Uses ID property newStudent.Name = “Robert Ekblaw” 'Let's say I get an A in this course newStudent.ReCalcGPA(3, 4.0) ' CSI101 is 3-hr course 'Question: What is result of newStudent.CanGraduate() ?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.