Consider the following class definition with two public members:
Public Class Point Public x As Integer Public y As Integer End Class
Instead of exposing two public members, it would be better to expose them as
properties. To do so, right-click on
x and select Refactor -> Encapsulate
Field (see Figure 19).
Figure 19. Encapsulating a variable as a field
The resultant class would look like this:
Public Class Point Public Property X() As Integer Get Return _x End Get Set(ByVal value As Integer) _x = value End Set End Property Private _x As Integer Public y As Integer End Class
There is one interesting aspect of how Refactor works. Instead of declaring the two pubic members separately, say you declare them in one single statement:
Public Class Point Public x, y As Integer End Class
If you now use Refactor to encapsulate
x as a field, you get the following
(notice that now
_x is of
Public Class Point Public Property X() As Integer Get Return _x End Get Set(ByVal value As Integer) _x = Value End Set End Property Private _x As Object Public y As Integer End Class
Often time we write code that test for the equality (or inequality) of expressions. Consider the following:
If num < 0 Then MsgBox("Negative") Else MsgBox("0 or positive") End If
You can reverse the condition by highlighting the first condition and then selecting Refactor -> Reverse Conditional (see Figure 20).
Figure 20. Reversing the logic of your code
The result would be:
If num >= 0 Then MsgBox("0 or positive") Else MsgBox("Negative") End If
The following block of code should be familiar to most people:
Dim obj As Object If Not (obj Is Nothing) Then ... End If
To simplify the expression and aid readability, you can right-click on the expression and then select Refactor -> Simplify Expression (see Figure 21).
Figure 21. Simplifying an expression
The code would be simplified as:
Dim obj As Object If obj IsNot Nothing Then ... End If
Replace Temp with Query
Consider the following block of code:
'---Volume of Cylinder Dim radius As Single = 3.5 Dim volume As Single = VolumeOfCylinder(height, radius) MsgBox(volume)
You can replace the volume variable with a method. To do so, right-click on the
volume variable and select Refactor -> Replace Temp with Query (see Figure
Figure 22. Replacing a variable with a method call
The code block now becomes (with a newly created
Private Function GetVolume( _ ByVal height As Single, _ ByVal radius As Single) As Single Return VolumeOfCylinder(Height, radius) End Function '---Volume of Cylinder Dim radius As Single = 3.5 MsgBox(GetVolume(height, radius))
Split Temporary Variable
Often times you declare a single variable and use it for a variety of purposes.
This is error-prone and makes debugging difficult. Consider the following (
used as loop variant as well as a placeholder for the
Dim i As Integer For i = 0 To 10 ... Next i = InputBox("Enter a number")
i and select Refactor -> Split Temporary Variable (see Figure
Figure 23. Splitting a temporary variable
The resultant code will be:
Dim i As Integer For i = 0 To 10 ... Next Dim lSplitI As Integer lSplitI = InputBox("Enter a number")
With the new Refactor for Visual Basic 2005, there is now no reason why you can say Visual Basic is not for professional developers. What is best is that all Visual Studio 2005 users can download this fantastic tool for free. What are you waiting for? Start coding and improve your code today!
Wei-Meng Lee (Microsoft MVP) http://weimenglee.blogspot.com is a technologist and founder of Developer Learning Solutions http://www.developerlearningsolutions.com, a technology company specializing in hands-on training on the latest Microsoft technologies.
Return to ONDotNet