Resident Engagement – feedback from first stage and strategic level proposals 2 nd stage consultation events March 2012
OOP in VB
OOP Principles
• An object – in memory – has– Some data values members– Pieces of executable code methods
• Objects usually come and go during execution – dynamic not static
• Objects belong to a class – the type of the object
• An object is an instantiation of the class
OOP Principles- Encapsulation
• Contrast with structured programming, where global data is processed by functions
• Encapsulation = enclosing the data in an object• Other code (in other classes) cannot change the
data in an object• No global data in pure OOP• High level of modularity• Data members should be private• Accessor methods of the class control access• Changes are (should be) validated
Typical classes (objects are nouns)
• Application-specific classes –– employee, invoice, contract, purchase order– dust particle in graphics for simulation
• System classes– UI – window, form, button, slider– System – thread, file, socket
Inheritance
• A class can be designed as a variation on another class (the base or ancestor class)
• The subclass inherits all the members and methods of the base class
• Supports code re-use – do not have to start from scratch
Polymorphism
• Sub-classes can have new or different data members of methods
• So can have variations on a theme
• eg a button is a sub-class of a window
Object lifetime
• An object is 'made' at run-time
• Using the keyword new
• Good OOP languages have constructors which make new objects
• and destructors to end objects and free up memory
References• In VB (and Java) classes are reference types• References are (hidden) pointers• No (exposed) pointers in VB or Java• In VB use Set eg-
Dim myObject as myClass ' say what type it is
Set myObject = new myClass ' actually make one
Dim object2 as myClass ' another reference
Set object2 = myObject ' to the same object
Garbage collection
• GC is when memory manager reclaims memory occupied by objects no longer needed – reused as free memory
• In C++ can explicitly destroy objects – memory reclaimed then
• In VB – system counts the number of references – when =0, GC happens
• Only important for large arrays of large objects
Classes in VB
• Class defined in a class module (name = name of class)
• Inside the class module you– declare private data members– methods as public or private sub or functions– Property Get or Let to access private data
members
Add Class module to project
• In Project.. Add class module
• Change name..
Example – particle object
• Idea – simulate physical system containing many particles
• Each particle needs data members – position and velocity
• And methods – initialise, move, and draw
• An array of these objects represents a bunch of particles
Look at the OOPParticle project
• Run it• Look through the code• Try changing the number of particles, the
viscous drag and so on.• Develop it –
– All the particles are green– Add data members for their red green and blue colour
components– Initialise them as random– Change the draw method to use the colour
Interfaces• In VB, an interface is a single way of 'talking to' different
classes• Interface written just like a class, but with empty methods• different classes can implement methods in different
ways• gives polymorphism
InterfaceMethod AMethod BMethod C
Class 1Method A (version 1)Method BMethod C
Class 2Method A (version 2)Method BMethod C
Class 3Method A (version 3)Method BMethod C
Calling code –Class1.methodAClass2.methodAClass3.methodA
Example interface - personnel
• Dealing with Employees
• All have name, rateOfPay properties
• All need a pay method
• Managerial employees are monthly paid
• Clerical employees are hourly paid
The Employee interface
Private nameStr As StringPrivate payRate As DoublePublic Property Let name(val As String)End PropertyPublic Property Get name() As StringEnd PropertyPublic Property Get rateOfPay() As DoubleEnd PropertyPublic Property Let rateOfPay(val As Double)End PropertyPublic Sub pay()End Sub
The Manager ClassImplements EmployeePrivate nameStr As StringPrivate payRate As DoublePublic Property Get Employee_name() As StringEmployee_name = nameStrEnd PropertyPublic Property Let Employee_name(val As String)nameStr = valEnd PropertyPublic Property Get Employee_rateOfPay() As DoubleEmployee_rateOfPay = payRateEnd PropertyPublic Property Let Employee_rateOfPay(val As Double)payRate = valEnd PropertyPublic Sub Employee_pay()MsgBox (nameStr & " gets paid " & payRate)End Sub
Testing the Manager class
Dim emp1 As Manager
Set emp1 = New Manager
emp1.Employee_name = "John (manager)"
emp1.Employee_rateOfPay = 2010
emp1.Employee_pay
The Clerical Class (interface example)Implements EmployeePrivate nameStr As StringPrivate payRate As Double' extra data memberPrivate hours As Integer'extra propertiesPublic Property Let hoursWorked(val As Integer)hours = valEnd PropertyPublic Property Get hoursWorked() As IntegerhoursWorked = hoursEnd Property..rest same as Manager class except..Public Sub Employee_pay()MsgBox (nameStr & " gets paid " & payRate * hours)End Sub
Using the Clerical Class
Dim emp2 As ClericalSet emp2 = New Clericalemp2.Employee_name = "Luke (clerical)"emp2.Employee_rateOfPay = 8.5emp2.hoursWorked = 100emp2.Employee_pay
Interfaces are not class hierarchies
•There is no inheritance in VB6•There are no derived classes in VB•In the above, Clerical is not a sub-class of Employee•Members must be re-coded