Skip to main content

Past Blast

Featured Products

Windows Mobile Developer Controls
Windows Mobile Developer Controls
Stay in touch using the DEVBUSS RSS feeds.
 

News

Windows Mobile Developer Controls
Windows Mobile Developer Controls

Simulating Object Orientated Programming with eVB

Written by Christopher May  [author's bio]  [read 47003 times]
Edited by Derek

Download the code

Page 1  Page 2  Page 3  Page 4  Page 5 

On load of the form, the OSI Utilities package is used to create the collections that will hold our 3 properties; First Name, Last Name, and Annual Income.

The Constructor uses a private number “ciUniqueNumber” that will serve as each new instance’s “key” in the collection. When the Constructor function is called, it increments ciUniquenumber, and calls CreateCollectionMember passing in ciUniqueNumber. CreateCollectionMember adds a new member to all the property collections using standard values that will later be overwritten such as “Undefined”. Finally the constructor returns a string that consists of two parts: the object type, and the key of its properties in the collections. So at this point MyEmployee is equal to “;Employee::1;”. If I create another Employee instance, it would hold the string “;Employee::2;”.

So now we have objNew equal to “;Employee::1;”. The next line of code assigns a first name to our new object.

Code from Form1

Employee.setFirstName objNew, txtEmployeeFirstName.Text

Code from Employee

Public Function setFirstName(objEmployee, sFirstName)
Dim iID
If (IsEmployee(objEmployee)) Then
iID = getID(objEmployee)
If Not (iID = -1) Then
ccolFirstName.Remove (CStr(iID))
ccolFirstName.Add sFirstName, CStr(iID)
Else
MsgBox "Employee::setFirstName was passed an object with an invalid ID."
End If
Else
MsgBox "Employee::setFirstName was passed an invalid object."
End If
End Function

As you can see, we can’t actually call methods on our object, because our object is really just a string. Instead we need to call methods of an objects class. In this case we want to call the setFirstName method of an Employee. Employee.setFirstName takes as parameters an Employee object and the name to assign to that Employee. The first thing the function does is to check to make sure that objEmployee is really an “Employee” by searching the string objEmployee for the UNIQUE_EMPLOYEE_STRING. The next step is to get the Key or ID of the specific Employee object that was passed into the function. Once the Key is known, the function removes and then adds the updated sFirstName string with the same Key. The reason I had to remove the member and then add a new one is because I couldn’t find a way to edit a collection element using the OSI Utilities package. All done. Our Employee now has a first name! We continue, adding a LastName and AnnualIncome in a similar fashion before finally adding our new object to a form level collection.

ccolEmployees.Add (objNew)

Now remember that we aren’t actually adding an object to ccolEmployees. Rather, we are adding a string that is equal to “;Employee::1;” and all its properties are stored in collections inside our Employee class file at a key location of “1”.

Inheritance and Polymorphism

Now lets show off our inherited classes. We will add a new Intern to our program using the following code:

objNew = Intern.Constructor()
Intern.setFirstName objNew, txtInternFirstName.Text
Intern.setLastName objNew, txtInternLastName.Text
Intern.setHourlyWage objNew, txtInternHourlyWage.Text
ccolInterns.Add (objNew)

This implementation is pretty much identical to the Employee class, but inside the class file you can see how our inheritance works.

Private Const UNIQUE_INTERN_STRING = ";Intern::"
Private ccolHourlyWage As OSIUTIL.Collection
Private ciUniqueNumber As Integer

Private Sub Form_Load()
Set ccolHourlyWage = Factory.CreateObject("OSIUTIL.Collection")
End Sub

Public Function Constructor()
Dim sInternObject
ciUniqueNumber = ciUniqueNumber + 1
Call CreateCollectionMember(CStr(ciUniqueNumber))
sInternObject = UNIQUE_INTERN_STRING & CStr(ciUniqueNumber)
'Intern object inheritates from Employee
sInternObject = sInternObject & Employee.Constructor
'return the unique identifier to the caller
Constructor = sInternObject
End Function

If you look carefully you will notice that the only property collection we have in our Intern class is ccolHourlyWage. Where are the FirstName, LastName, and AnnualIncome properties that our Intern is supposed to be inheriting? Well the answer is found in the line of code:

sInternObject = sInternObject & Employee.Constructor

All the properties that an Employee has will remain stored in the Employee class, not only for all instances of Employee, but also for all instances of Employee’s descendents.

After Intern.Constructor has finished running it will return a string “;Intern::1;Employee::2;” indicating that our new object is an Intern, which inherited from Employee. Just as we did with our Employee, lets assign our new Intern some properties.

Previous Page  Next Page