The Loan class module doesn't actually create an object; it just provides a blueprint, or a definition, that lets your program stamp out one or more real Loan objects as needed. Let's complete the example by adding the LOAN.CLS module to a simple project and doing something with it.
Start with a new Standard EXE project containing just a single form. I'll keep this form very simple as its only purpose is to create and demonstrate a Loan object. Change the form's Caption property to Please Click on This Form. Add the LOAN.CLS class module created in the previous section to your project, and add the following lines of code to your form. Following the code listing are explanations of the different parts of this code.
Option Explicit
Private Sub Form_Click()
Dim loanTest As New Loan
Dim intCount As Integer
Dim curBalance() As Currency
`Clear the face of the form
Cls
`Set loan parameters
loanTest.Reset
loanTest.Principal = 1000
loanTest.Months = 12
loanTest.AnnualInterestRate = 8.5
`Display parameters used
Print "Principal: ", , Format(loanTest.Principal, "Currency")
Print "No. Months: ", , loanTest.Months
Print "(Years:) ", , loanTest.Years
Print "Interest Rate:", , Format(loanTest.AnnualInterestRate _
/ 100, "Percent")
Print "Monthly Payment: ", Format(loanTest.Payment, "Currency")
Print
`Get array of monthly balances
curBalance() = loanTest.Balance()
`Display payment schedule
For intCount = LBound(curBalance) To UBound(curBalance)
Print "Month: "; intCount,
Print "Balance: "; Format(curBalance(intCount), "Currency")
Next intCount
End Sub
Figure 5-1 shows the form during development.
Figure 5-1. The form used to demonstrate the Loan object.
A new Loan object named loanTest is created when you click on the form, as shown in the code below. The Loan object will be destroyed automatically, just like any other local variable, when it goes out of scope.
Dim loanTest As New Loan
The following three lines assign values to three of the properties we've created for the Loan object. Review the Loan class module to see how the Months property works differently from the Principal and AnnualInterestRate properties.
loanTest.Principal = 1000 loanTest.Months = 12 loanTest.AnnualInterestRate = 8.5
As you output results, various properties of the Loan object are accessed as required. This is shown in the following code. In most cases, the loanTest object simply hands back each value as it is currently stored in the object. In the case of the loanTest.Payment property, however, a complex calculation is triggered. The calling code is blissfully unaware of when, where, and how the calculations are performed.
Print "Principal: ", , Format(loanTest.Principal, "Currency")
Print "No. Months: ", , loanTest.Months
Print "(Years:) ", , loanTest.Years
Print "Interest Rate:", , Format(loanTest.AnnualInterestRate _
/ 100, "Percent")
Print "Monthly Payment: ", Format(loanTest.Payment, "Currency")
Print
Calling the Balance property, shown here, tells the loanTest object to prepare and return an array of loan balances, in preparation for outputting each month's balance in the next few lines of the program:
`Get array of monthly balances curBalance() = loanTest.Balance()
The last few lines of our sample program display an amortized table of the loan's balance at the end of each month of the loan:
`Display payment schedule
For intCount = LBound(curBalance) To UBound(curBalance)
Print "Month: "; intCount,
Print "Balance: "; Format(curBalance(intCount), "Currency")
Next intCount
When the end of the Form_Click event procedure is reached, all variables declared in the procedure go out of scope and are automatically destroyed by the system. This includes the instance of the Loan object, with all its code and data.
Figure 5-2 shows the results of running this program. Notice that you could greatly improve and expand on this simple program without ever having to touch the contents of the Loan class module.
A carefully designed object, in the form of a class module, lets you extend Visual Basic by adding some new programmable objects to your bag of tricks. Once you get an object the way you want it, all you need to document for future reference and use is the object's interface, which is its set of public properties and methods. The internal workings and implementation of the object can be mentally "black-boxed" and ignored. Imagine the object encased in a hard shell, with just the defined interface visible from the outside world. This is what is meant by the object-oriented programming term encapsulation. The object itself is responsible for the correct implementation of its features and behavior, and the calling application is responsible for understanding and correctly using the object's interface only. This encapsulation makes it much easier to create and debug larger applications. That demonstrates the true power and beauty of object-oriented programming!
Figure 5-2. Output generated by using a Loan object.
The Loan class was simply added as another module to the sample application. This is a great way to use class modules, but you can also combine one or more class modules into an ActiveX component.
SEE ALSO
- The section "Dear John, How Do I... Create and Use an ActiveX EXE?" later in this chapter, and Chapter 27, "Advanced Programming Techniques," for more information about ActiveX components
- The Lottery application in Chapter 29, "Graphics," for a demonstration of the use of objects