Refactoring some common VB.NET Code

Refactoring some common VB.NET Code

One of my tasks at work is to maintain a few VB.NET applications written before I started working here. I’ve had the opportunity to learn a lot about large websites using VB.NET.

I thought I’d take a few moments to point out some common refactoring that you can do to shorten your code when dealing with If statements and boolean assignments.

Let’s start with a line of code that appends some text to an existing string if certain conditions are true.

If chkRailCar.Checked = True Then C1 = C1 & "Y" Else C1 = C1 & "N"

This is simple enough, but it’s a bit redundant. Let’s look at another way of expressing this.

C1 &= IIf(chkRailCar.Checked, "Y", "N")

This uses the &= operator which just tells VB to append whatever follows to the variable on the left. We use IIf to determine what value to set based on the state of the checkbox.

Another common situation is using the value of a variable to determine the state of a boolean property. Here is some code to illustrate the “old” way:

If Mid(C1, 40, 1) = "Y" Then C1GridView1.Columns.ColumnByName("Age Allocated").Visible = True Else C1GridView1.Columns.ColumnByName("Age Allocated").Visible = False End If

This has some redundant code in it that is begging to be refactored. Let’s take a look at how we can accomplish this same amount of logic in a single line of code:

C1GridView1.Columns.ColumnByName("Age Allocated").Visible = Mid(C1, 40, 1) = "Y"

This tells VB to set the Visible property to the boolean value of the result of the expression to the right of the first = sign. VB will then evaluate the expression Mid(C1, 40, 1) = “Y” to come up with either True or False, which it will then assign to the Visible property. So, if Mid(C1, 40, 1) = “Y”, then the Visible property will be set to True.

Instead of using “Flags” (Dim myFlag as String = “Y”), try using Boolean values instead. “Y” and “N” are strings and must be manually converted to a boolean when setting properties. I’ve never seen a control use a “Y” or “N” as a property setting. They use True and False.

Sometimes, you need a short representation of true and false that can be easily cast to a boolean. In these cases, use an integer of 1 and 0 to represent true and false. One easy way to remember which is which is to remember that “non zero equals true”. That would also include -1 to equal true.

If you need a flag with multiple values, create an Enumeration like so:

Public Enum trafficLightEnum Red Yellow Green End Enum

Then, just use it in your If statements:

Dim lightState as trafficLightEnum lightState = trafficLightEnum.Yellow If lightState = trafficLightEnum.Red Then Vehicle.Stop() Else If lightState = trafficLightEnum.Green Then Vehicle.Go() Else If lightState = trafficLightEnum.Yellow Then Vehicle.FloorIt() End If

Check out Microsoft’s documentation on Enumerations. They are very useful.

VB.NET brings with it so many improvements to the Visual Basic language of days-gone-by. It’s worth it to get out there and experiment a little. Try something new. Don’t settle on what you remember doing from your old VB 6 days.