VB Coding in a .NET World

C# is the standard coding language for developers at ImageSource, but it wasn’t always that way.  Before, VB.NET was the predominant language of choice.  When converting some of the codebases over from VB.NET, we consistently run into areas where VB.NET specific commands have been used.  These commands either do not exist in C# or require use of the Microsoft.VisualBasic library.  Either make code conversion very frustrating when they are used.

Below are some examples of commands that should be avoided in .NET code:

Cint, Cstr, CDate, CDbl

Use the equivalent call in the System.Convert class.

  • CInt – Convert.ToInt32
  • CStr – Convert.ToString
  • CDate – Convert.ToDateTime
  • CDbl – Convert.ToDouble
IsDate, IsNumeric

This one is a bit trickier and requires more work, but can be a lot more useful.  C# does not have an equivalent type checker, but you can use the TryParse methods located in the primitive objects.  For example, the following VB.NET code can be used to check if a string is an integer value.

  • Dim i As Integer = 0
    Dim test As String = "213"
    If Integer.TryParse(test, i) Then
      'Hey, this check worked!
      'And the integer has already been converted so you
      'can start using it.
    End If

The new line command in VB, necessary because VB.NET does not allow you to write special characters in strings, so special characters need to be concatenated onto the strings.  Instead of VBCRLF, use the Environment.NewLine command, as this exists in both VB.NET and C#.

  • Optimally, code converted to C# should convert any instances of VBCRLF to \n in the string.  However, using Environment.NewLine will at least get it to compile without need for the Microsoft.VisualBasic library.

Avoid the With statement, as it does not exist in C# and you’ll just end up having to put the object reference in anyway.  It is not that much harder to just write the objectName.Method or objectName.Property, and it makes the code easier to understand.

So hopefully this brief overview will help you to write better VB.NET code.  Remember, the .NET framework exists in both VB and C# form, and making the two easily compatible helps code maintainability and portability.

One thought on “VB Coding in a .NET World

  1. Something else which we consistently run into when upgrading our VB6 code is the IFF statement. Fortunately, this is no longer exists in VB.NET (at least, I can’t find it). I think the appeal of this statement was to reduce the number of lines of code, but it created unnecessary complexity.

    This complexity contributes to errors.

    More lines of simpler code is *much* easier to maintain than fewer lines of more complex code. No serious programmer would disagree.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s