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 

In a true OO language, each object would have its own properties and methods that you could call against the object itself such as MyObject.MyMethod(). However, because eVB does not allow you to create instances of a class as in VB, I had to come up with another way to simulate the idea behind OO programming. If the object doesn’t know what its properties are, then who will? The answer is: the class file. From project to project, if you are planning on reusing a class you don’t want to have to add additional files and code to keep track of your objects. You want to drop one file into your project and have the functionality of that class immediately available. For each property of a class, I create a private collection in my class file which will hold all values for all instances of the class. So rather than having the object know its properties, it will simply need to know its “key” in the classes collection’s. In order to achieve polymorphism and inheritance, each object will need to know what type of object it is, and what classes it inherits from. In order to have our object know both its type and its key, I decided to make our “object” a string that contains the necessary information.

The example program

In today’s job market, executives need to be up to date on the current payroll expenses, and be able to quickly enact cost cutting measures. So I developed a “Pocket Salary Cutter” application do demonstrate the use of objects in eVB. I decided that I would need an Employee class that would have properties of First Name, Last Name, and Annual Income. In addition to the general Employee, we need two specific kinds of employees: Interns and Managers. Interns are paid based on an hourly wage, and Managers have an Annual Bonus in addition to their Salary. Because both Interns and Managers ARE Employees, we can have the Intern and Manager classes inherit from our Employee class. Using the Pocket Salary Cutter application, lets add an employee to our collection.

Private ccolEmployees As OSIUTIL.Collection
Private ccolInterns As OSIUTIL.Collection
Private ccolManagers As OSIUTIL.Collection

Private Sub cmdAddEmployee_Click()
Dim objNew
If Me.optEmployee.Value = True Then
objNew = Employee.Constructor()
Employee.setFirstName objNew, txtEmployeeFirstName.Text
Employee.setLastName objNew, txtEmployeeLastName.Text
Employee.setAnnualIncome objNew, txtEmployeeAnnualIncome.Text
ccolEmployees.Add (objNew)
.
.
.
End Sub

When the form is loaded, the collections are created for use throught the form. When the user clicks on cmdAddNewEmployee the Employee.Constructor is called.

Option Explicit
'******************************
'* Employee Class is a Base Class
'******************************

Private Const UNIQUE_EMPLOYEE_STRING = ";Employee::"

Private ciUniqueNumber As Integer

Private ccolLastName As OSIUTIL.Collection
Private ccolFirstName As OSIUTIL.Collection
Private ccolAnnualIncome As OSIUTIL.Collection

Private Sub Form_Load()
'create Collections
Set ccolLastName = Factory.CreateObject("OSIUTIL.Collection")
Set ccolFirstName = Factory.CreateObject("OSIUTIL.Collection")
Set ccolAnnualIncome = Factory.CreateObject("OSIUTIL.Collection")
End Sub

Public Function Constructor()
ciUniqueNumber = ciUniqueNumber + 1
'Add another member for the collections
CreateCollectionMember (CStr(ciUniqueNumber))
'return the unique identifier to the caller
Constructor = UNIQUE_EMPLOYEE_STRING & CStr(ciUniqueNumber) & ";"
End Function

Private Sub CreateCollectionMember(sNewKey)
ccolFirstName.Add "Undefined", CStr(sNewKey)
ccolLastName.Add "Undefined", CStr(sNewKey)
ccolAnnualIncome.Add CDbl(0), CStr(sNewKey)
End Sub

Previous Page  Next Page