Add / Remove Line Numbers (VB.NET)

Top  Previous  Next

The Add / Remove Line Numbers (VB.NET) feature allows you to add or remove line numbers to the source code of a procedure, class, file, project, or solution. This allows you to use the Erl function of VB.NET in your error handlers to know the exact line number that caused the error:

 

Private Sub Foo()

 

10:   Dim o As Object
 
20:   On Error GoTo Proc_error
30:   MsgBox(o.ToString)
40:   Exit Sub

 

Proc_error:

 

50:   MsgBox("Error " & Err.Description & " in line " & Err.Erl & " of Foo")

 

End Sub

Although VB.NET allows you to use structured error handling using the Try / Catch statement as follows:

 

Private Sub Foo()

 

Dim o As Object

 

Try

MsgBox(o.ToString)

Catch ex As Exception

MsgBox(ex.ToString)

End Try

 

End Sub

you may want to use the old error handling mechanism of VB 6.0 in the following scenarios:

  • You have migrated legacy code from VB 6.0 and you want to use the same error handling code.
  • You want to know the line that caused an error even in the Release configuration, since the ToString() method of the Exception class only shows the file name and the line number of the error in the Debug configuration (which generates a .pdb debug file). In the Release configuration the .pdb file is missing, and only the class and procedure name are shown in the exception message, but not the specific line number.

MZ-Tools offers the following ways to add / remove line numbers:

  • The MZ-Tools | Other Utilities | Add Line Numbers (VB.NET)... and MZ-Tools | Other Utilities | Remove Line Numbers (VB.NET)... menus.
  • The Other Utilities | Add Line Numbers (VB.NET)... and Other Utilities | Remove Line Numbers (VB.NET)... menus on the MZ-Tools toolbar.
  • The keyboard shortcut associated with the MZTools6_VSXXXX.AddLineNumbers and MZTools6_VSXXXX.RemoveLineNumbers commands.

When you invoke this feature, a dialog window is shown that allows you to select the scope of the operation.

Remarks:

  • Ensure that the source code can be compiled without errors before running this feature.
  • Visual Basic does not allow line numbers greater than 65535 (they are treated as 0), so MZ-Tools resets the numeration when it reaches that limit. Take this into account if you use unique numbers (rather than numbers that reset at procedure or file level) when numbering a whole project.
  • Visual Basic does not allow a line number in the first Case statement of a Select Case block. If you try to add the line number by hand and you hit F5 to run the project, you get the VB error: "Statements and labels invalid between Select Case and first Case". So, MZ-Tools does not number the first Case statement of a Select Case block to avoid that VB error.

See Also:

Add Line Numbers (VB.NET) Options

Keyboard Shortcuts Options