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 

From Form1

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

From Intern

Public Function setLastName(objIntern, sLastName)
Call Employee.setLastName(objIntern, sLastName)
End Function

Public Function setHourlyWage(objIntern, iHourlyWage)
Dim iID
If (IsIntern(objIntern)) Then
iID = getID(objIntern)
If Not (iID = -1) Then
ccolHourlyWage.Remove (CStr(iID))
ccolHourlyWage.Add iHourlyWage, CStr(iID)
Call Employee.setAnnualIncome(objIntern, (iHourlyWage * 52 * 40))
Else
MsgBox "Intern::setHourlyWage was passed an object with an invalid ID."
End If
Else
MsgBox "Intern::setHourlyWage was passed an invalid object."
End If
End Function

Now, when we call methods of any Intern that are inherited from Employee, all we have to do is pass down the parameters to Employee as shown in the function setLastName. When we are setting a value for a new parameter such as HourlyWage, we use the same type of functions to enter the value into the correct Key in the collection. In addition for the setHourlyWage function, we use the employees hourly wage to determine their AnnualIncome, and pass that along to Employee as well. By making sure all descendants of Employee have an AnnualIncome property, we will be able to use polymorphism later in this article.

Lets head back to our program. Our executives need to cut costs on the go, so we have been asked to add a method to the Intern class that will allow executives to cut their hourly rate whenever and wherever they want! So inside the Intern class we add a new method called GivePayCut:

Public Sub GivePayCut(objIntern, iHourlyPayCut)
Dim iNewHourlyWage As Integer
Dim iOldHourlyWage As Integer
Dim iID As Integer
If (IsIntern(objIntern)) Then
iID = getID(objIntern)
If Not (iID = -1) Then
iOldHourlyWage = Intern.getHourlyWage(objIntern)
If iOldHourlyWage > 0 Then
iNewHourlyWage = iOldHourlyWage - iHourlyPayCut
If (iHourlyPayCut > 0) And (iNewHourlyWage >= 0) Then
Call Intern.setHourlyWage(objIntern, iNewHourlyWage)
Else
MsgBox "Error. Intern::GivePayCut was passed iHourlyPayCut=" & iHourlyPayCut
End If
End If
Else
MsgBox "Intern::setHourlyWage was passed an object with an invalid ID."
End If
Else
MsgBox "Intern::GivePayCut was passed an invalid object."
End If
End Sub

Lets see the new method in action. After adding all our employees inside the program, we click on “Cut Costs” which populates a list with all Interns from the ccolInterns collection.

The intern that is making the most money is given a $1.50 pay cut by clicking on the “Reduce Hourly Wage” button.

Private Sub cmdReduceWage_Click()
If Len(Me.txtWageReduction.Text) > 0 Then
If (lstInterns.ListIndex >= 0) Then
Call Intern.GivePayCut(ccolInterns.Item(lstInterns.ListIndex + 1), CDbl(Me.txtWageReduction))
Call cmdDisplayAnnualIncome_Click
Else
MsgBox "Please select an intern."
End If
Else
MsgBox "Please enter a dollar amount."
End If
Me.txtWageReduction.Text = ""
End Sub

When we look again, we see that indeed, the company is now saving an extra $1.50 for every hour that James Blake works.

Previous Page  Next Page