Chapter 4: Learning Just Enough C# and VB.NET: Intermediate Syntax | 105 |
VB:
Sub ProcessPayrollForAllAccounts()
Dim accounts As IAccount() = GetAllAccounts()
For Each account In accounts
account.Credit(1000)
Next
End Sub
Function GetAllAccounts() As IAccount()
Dim allAccounts(3) As IAccount
allAccounts(0) = New Checking() allAccounts(1) = New Saving() allAccounts(2) = New Checking() allAccounts(3) = New Saving()
Return allAccounts
End Function
You can call the code in Listing
C#:
Program bank = new Program(); bank.ProcessPayrollForAllAccounts();
VB:
ProcessPayrollForAllAccounts()
Examining Listing
Looking inside of the GetAllAccounts method, you can see how an array is being built with both Checking and Saving objects. Since Checking and Saving implement IAccount, which you saw in Listings
Back in the ProcessPayrollForAllAccounts method, you can see a loop iterate through each IAccount instance, calling Credit. The reason you can call Credit like this is that IAccount defines a contract for the Credit method. Calling Credit on each instance really