1 / 21

Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul # 5

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)

mkeen
Download Presentation

Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul # 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Enterprise Development Using Visual Basic 6.0 Autumn 2002Tirgul #5 ‘Tirgul’ # 5

  2. Objectives • Understanding objects • Class Modules • Properties Get/Let • Defining Types • Collections ‘Tirgul’ # 5

  3. 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

  4. 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

  5. The Snail Abstraction timeToCrewel(Dist as integer) as integer age goToSleep() IsSleep setSpeed(Speed as integer) Color Gender Properties Functions ‘Tirgul’ # 5

  6. 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

  7. 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

  8. 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

  9. Property Get • Gets a value of a property • Syntax: Dim sName as String PropertyGet Name() as String Name = sName End Property ‘Tirgul’ # 5

  10. 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

  11. 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

  12. 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

  13. Class can use class ‘Tirgul’ # 5

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. Class Builder • click Project Add Class module and select VB Class Builder: ‘Tirgul’ # 5

  20. 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

  21. Add Method ‘Tirgul’ # 5

More Related