.NET Framework 2.0 Performance Inspection Questions - Visual Basic Considerations

From Guidance Share

Jump to: navigation, search

- J.D. Meier, Srinath Vasireddy, Ashish Babbar, Rico Mariani, and Alex Mackman


Visual Basic Considerations

When optimized, Visual Basic .NET code can perform as well as C# code. If you have ported existing Visual Basic code to Visual Basic .NET, performance is unlikely to be optimized because you are unlikely to be using the best .NET coding techniques. If you have Visual Basic .NET source code, review the following questions:

  • Have you switched off int checking?
  • Do you use on error goto?
  • Do you turn on Option Strict and Explicit?
  • Do you perform lots of string concatenation?

Have You Switched Off int Checking?

Int checking is beneficial during development, but you should consider turning it off to gain performance in production. Visual Basic turns on int checking by default, to make sure that overflow and divide-by-zero conditions generate exceptions.

Do You Use On Error Goto?

Review your code to see if it uses the on error goto construct. If it does, you should change your code to use the .NET structured exception handling with Try/Catch/Finally blocks. The following code uses on error goto.

  Sub AddOrderOld(connstring)
      On Error GoTo endFunc
      Dim dataclass As DAOrder = New DAOrder
      Dim conn As SqlConnection = New 
      If Not(conn is Nothing) Then
      End If
  End Sub

The following code shows how this should be rewritten using exception handling.

  Sub AddOrder(connstring)
     Dim conn As SqlConnection
       Dim dataclass As DAOrder = New DAOrder
       conn = New SqlConnection(connstring)
     Catch ex As Exception
       ' Exception handling code
      If Not(conn is Nothing) Then
      End If
     End Try
  End Sub

Do You Turn on Option Strict and Explicit?

Review your code and ensure that the Strict and Explicit options are turned on. This ensures that all narrowing type coercions must be explicitly specified. This protects you from inadvertent late binding and enforces a higher level of coding discipline. Option Explicit forces you to declare a variable before using it by moving the type-inference from run time to compile time. The code for turning on Explicit and Strict is shown in the following code sample.

  Option Explicit On 
  Option Strict On

If you compile from the command line using the Vbc.exe file, you can indicate that the compiler should turn on Strict and Explicit as follows.

  vbc mySource.vb /optionexplicit+ /optionstrict+

Do You Perform Lots of String Concatenation?

If your code performs lots of string concatenations, make sure that it uses the StringBuilder class for better performance.

Note If you use ASP.NET to emit HTML output, use multiple Response.Write calls instead of using a StringBuilder.

Personal tools