Chapter 4: Learning Just Enough C# and VB.NET: Intermediate Syntax

103

Function GetCheckingAccounts() As Checking()

Dim chkAccts(1) As Checking

chkAccts(0) = New Checking() chkAccts(1) = New Checking()

Return chkAccts

End Function

Function GetSavingsAccounts() As Saving()

Dim numberOfAccounts As Integer = 5

Dim savAccts(numberOfAccounts) As Saving

For i As Integer = 0 To numberOfAccounts savAccts(i) = New Saving()

Next

Return savAccts

End Function

To save space, I haven’t included the entire application in Listing 4-5, which is available with the source code for this book via the McGraw-Hill Web site. To understand how it works, imagine that you’ve written the following code in the Main method:

C#:

Program bank = new Program(); bank.ProcessPayrollForCheckingAndSavingAccounts();

VB:

ProcessPayrollForCheckingAndSavingAccounts()

Walking through the code, let’s start at the ProcessPayrollForCheckingAndSaving Accounts method. You can see how the algorithm calls GetCheckingAccounts to retrieve an array of Checking objects. If you recall, an array is a list of elements of a specified type, that type being Checking in this case. The algorithm goes on to iterate through the Checking objects, invoking Credit on each to add 500 to the account. Some employees want their paychecks in Checking, but others might want their paychecks to go into Saving (or some other account). Therefore, the algorithm calls GetSavingsAccounts to get a list of those accounts for employees who want their paychecks to go into their savings. You’ll notice that the algorithm inside of GetSavingsAccounts is different from

Page 126
Image 126
Microsoft 9GD00001 manual 103