‘Tirgul’ # 5 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #5
‘Tirgul’ # 5 Objectives Understanding objects Class Modules Properties Get/Let Defining Types Collections
‘Tirgul’ # 5 Object Classes Objects contains: –Data –Behavior Controls are objects –data (properties) –behavior(Functions and events) All objects of the same type has the same data and behavior
‘Tirgul’ # 5 The Class Module A class is a template: it defines the data and behavior of the objects that belong to it. An instance is a variable associated with an object (a specific instance of a class).
‘Tirgul’ # 5 The Snail Abstraction age Color Gender timeToCrewel(Dist as integer) as integer IsSleep Properties Functions setSpeed(Speed as integer) goToSleep()
‘Tirgul’ # 5 Multiple instances of a class All Objects has the same properties and behavior But not same values Class Car Color as Colur Speed as Double Size as Double Color = red Speed = 200 Size = Color = Yellow Speed = 90 Size = Color = Blue Speed = 120 Size = 160.7
‘Tirgul’ # 5 Property Methods Provide access to the instance variables can assign or retrieve the value of an instance variable property –A named attribute of an object. Define object characteristics such as size, color, and screen location, or the state of an object, such as enabled or disabled.
‘Tirgul’ # 5 Property Let Assign a value to a class property Syntax: Optionally: –Use ByVal retain the value of the parameter Dim sName as String Property Let Name(ByVal newName As String) sName = newName End Property
‘Tirgul’ # 5 Gets a value of a property Syntax: Property Get Dim sName as String Property Get Name() as String Name = sName End Property
‘Tirgul’ # 5 Property Set Sets a value to an Object Syntax: Use Dim mvartest as Variant Public Property Set test(ByVal vData As Variant) Set mvartest = vData End Property Set x.test = Form1
‘Tirgul’ # 5 Class Initialization Important for defaults Will be called automatically at each new Instance Private Sub Class_Initialize() CTN = “ ” age = 0 fName = “” End Sub
‘Tirgul’ # 5 Behavior Functions Defines the behavior of the object Done same as form functions/Subs Public will be used by the user Private will be hidden for the user Public function getSalary(code as integer) as double getSalary = getSalaryByCode(code) End function Private function getSalaryByCode(code as integer) as double getSalaryByCode = rsSalary.Field(code).value End function
‘Tirgul’ # 5 Class can use class
‘Tirgul’ # 5 Declaring an Object Variable Dim myStudent as cStudents … Set myStudent = new cStudents You need to define the object and create new instance. Could be done in the same line but better to separate Set: Assign an object reference to variable Dim myStudent as new cStudents
‘Tirgul’ # 5 Collections A very useful object to hold objects Unlike List, can hold Objects Functions –Add – Adding objects –Item(index) – retrieve the Item in the index –Count – Number of items –Remove(Index) – remove an Item in the index Dim Emp1 as new cEmployee Dim Emp2 as new cEmployee Dim Factory As New Collection Factory.Add Emp1 Factory.Add Emp2
‘Tirgul’ # 5 Retrieves a specific Item from the collection Item Method For index = 1 To Factory.Count Debug.Print Factory.Item(index).Name Next index Property of the item
‘Tirgul’ # 5 Programmer defined Data Types Type is a degenerate class In a form (private) or module(Public) Useful to hold data in a logic group Referenced as a base type (no New) Public Type Point x as Integer Y as Integer End Type Dim myPoint as Point myPoint.x = 1 myPoint.y =
‘Tirgul’ # 5 Source Examples prjEmployee – Simple class use prgStudent – Properties use prgRoll_Call - Class_Initialize, Container class prgOrderItem – with reserved word prgBankAccount – Class functions prgArithmeticSeries Lec3LinkList – List example
‘Tirgul’ # 5 Class Builder click Project Add Class module and select VB Class Builder:
‘Tirgul’ # 5 Add Method select File, New, Method. Enter Method name, click OK. You should now see the Method in the right hand pane Update the project click File, Exit.
‘Tirgul’ # 5 Add Method