At this point, you might decide that you have a near-perfect method declaration, but you want to change the order of the parameters. To accomplish this, move to one of the variables in the function declaration:
Private Function GetMonthlyPayment(ByVal InterestRate As Double, _ ByVal Years As Double, ByVal FinalValue As Double) As Double
When you select the refactoring tag, you'll see the option for reordering the parameters. Choose this. You'll now be able to slide variables from one place to another using the left and right arrow keys. When you've rearranged them the right way, press Enter. Here's one possibility:
Private Function GetMonthlyPayment(ByVal Years As Double, _ ByVal FinalValue As Double, ByVal InterestRate As Double) As Double
The refactoring tool will show you all the places in the code where you call this method, and you can choose to change them individually (click on the checkmark icon) or all at once (click on the double checkmark icon). Without this tool, you'd be exposed to the possibility of error. Because all the variables are the same type, you might inadvertently keep a piece of code that calls the method but has these values in the wrong order.
The last trick I'll cover allows you to make your code more explicit by breaking
down log lines of code. Currently, the
Pmt() function performs an inline interest rate
MonthlyPayment = Pmt(InterestRate / 12 / 100, _ Months, 0, -FinalValue, _ DueDate.BegOfPeriod)
It might clarify your code to expand that with a separate variable. (And even if it doesn't, keep in mind that these tools are great for examples where your code is that much more complex--we've all seen hideously tangled lines of code that do too much at once.)
To refactor this code, move to the interest rate calculation and choose the refactoring
Introduce Local. You'll be prompted to name a new local variable, and will end up
with this code:
Dim MonthlyInterestPercent As Integer = InterestRate / 12 / 100 MonthlyPayment = Pmt(MonthlyInterestPercent, _ Months, 0, -FinalValue, DueDate.BegOfPeriod)
You can use a similar technique to replace hardcoded values with constants.
To learn more about refactoring, surf over to www.devexpress.com/Support/BestPractices/IDE/Refactor, which has an exhaustive list of refactoring features. Or, better yet, download the Refactor 2005 tool and start experimenting.
In April 2005, O'Reilly Media, Inc., released Visual Basic 2005: A Developer's Notebook.
Sample Chapter 2: The Visual Basic Language (PDF) is available free online.
For more information, or to order the book, click here.
Matthew MacDonald is a developer, author, and educator in all things Visual Basic and .NET. He's worked with Visual Basic and ASP since their initial versions, and written over a dozen books on the subject, including The Book of VB .NET (No Starch Press) and Visual Basic 2005: A Developer's Notebook (O'Reilly). His web site is http://www.prosetech.com/.
Return to the Windows DevCenter.