WindowsDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


AddThis Social Bookmark Button

Refactoring in Visual Basic 2005
Pages: 1, 2, 3

In this example, choose Extract Method from the refactoring smart tag. A red arrow will appear in the editor window, which you can move using the up and down arrow keys (Figure 2). Once you've found the place where you want to create the new method, press the Enter key.



Figure 2
Figure 2. Positioning the new method

The refactoring tool will create a new method and replace the code in Main() with the method call. The name of the method is generated automatically based on the containing method, with the word Extracted added to the end. So in this example, refactoring creates a method named MainExtracted(). However, the name is highlighted in green, and you can change it by typing in a more suitable choice. As you type, both the method name and the method call will be adjusted (Figure 3).

Figure 3
Figure 3. Naming the new method

Once you're finished, you'll end up with this unspectacular result:


Private Sub CalculateInterest()
    Dim InterestRate, Years, FinalValue, MonthlyPayment As Double
    Console.Write("Interest Rate: ")
    InterestRate = Val(Console.ReadLine())
    Console.Write("Years: ")
    Years = Val(Console.ReadLine())
    Console.Write("Final Value: ")
    FinalValue = Val(Console.ReadLine())
    Dim Months = Years * 12
    MonthlyPayment = Pmt(InterestRate / 12 / 100, _
      Months, 0, -FinalValue, _
      DueDate.BegOfPeriod)
    Console.WriteLine("Result: " & MonthlyPayment.ToString("C"))
End Sub

Sub Main()
    CalculateInterest()
End Sub

This gives you the starting point for rearranging your code, but it doesn't offer much more. The input code and the calculation code is still thrown unceremoniously together in the same method.

But now that you understand the basic technique, you can also do something much more useful. To try this out, undo your changes so that you return to the initial code version. Then select just the calculation code, which in this case constitutes the following lines:


Dim Months = Years * 12
MonthlyPayment = Pmt(InterestRate / 12 / 100, _
  Months, 0, -FinalValue, _
  DueDate.BegOfPeriod)

Extract this method. Now the refactoring tool gets much more intelligent. It notices that the values you're using--InterestRate and Years--are defined elsewhere in the method and thus need to be supplied to the method. It also spots the fact that the MonthlyPayment result is displayed afterward, and so must be a return value. The resulting code is much closer to what you want:


Sub Main()
    Dim InterestRate, Years, FinalValue, MonthlyPayment As Double
    Console.Write("Interest Rate: ")
    InterestRate = Val(Console.ReadLine())
    Console.Write("Years: ")
    Years = Val(Console.ReadLine())
    Console.Write("Final Value: ")
    FinalValue = Val(Console.ReadLine())
    MonthlyPayment = GetMonthlyPayment(InterestRate, Years, FinalValue)
    Console.WriteLine("Result: " & MonthlyPayment.ToString("C"))
End Sub

Private Function GetMonthlyPayment(ByVal InterestRate As Double, _
  ByVal Years As Double, ByVal FinalValue As Double) As Double
    Dim MonthlyPayment As Double
    MonthlyPayment = Pmt(InterestRate / 12 / 100, _
      Months, 0, -FinalValue, _
      DueDate.BegOfPeriod)
    Return MonthlyPayment
End Function

In this example, you don't even need to rename the extracted method. The refactoring tool realizes you're returning a value that you've named MonthlyPayment, and defaults to the sensible method name GetMonthlyPayment().

Pages: 1, 2, 3

Next Pagearrow