210 likes | 222 Views
Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul # 5. Objectives. Understanding objects Class Modules Properties Get/Let Defining Types Collections. Object Classes. Objects contains: Data Behavior Controls are objects data (properties) behavior(Functions and events)
E N D
Enterprise Development Using Visual Basic 6.0 Autumn 2002Tirgul #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 timeToCrewel(Dist as integer) as integer age goToSleep() IsSleep setSpeed(Speed as integer) Color Gender Properties Functions ‘Tirgul’ # 5
Multiple instances of a class Class Car Color as Colur Speed as Double Size as Double Color = red Speed = 200 Size = 100.5 Color = Blue Speed = 120 Size = 160.7 Color = Yellow Speed = 90 Size = 150.6 All Objects has the same properties and behavior But not same values ‘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 AsString) sName = newName End Property ‘Tirgul’ # 5
Property Get • Gets a value of a property • Syntax: Dim sName as String PropertyGet 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 = “050000000” 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 • 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 Dim myStudent as cStudents … Set myStudent = 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
Item Method • Retrieves a specific Item from the collection • 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 = 2 4 3 2 1 1 2 3 4 ‘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 ‘Tirgul’ # 5