We Are GAP Mobilize
Free Assessment Tool

VB to .NET

Avoid Substring Out of Bounds

VB6 Left & Right functions allowed the usage of out-of-bounds indexes, automatically replacing them with the actual lower/upper bound as required. Converting these methods directly to “Substring” invocations could cause the firing of out-of-bounds exceptions which were not triggered in VB6.

If this optional feature is enabled the Visual Basic Upgrade Companion will add Math.Max and Math.Min invocations to the resulting code in order to avoid out-of-bounds exceptions. This is a brief usage example:

Original VB6 Code:

Private Sub method1()
Dim String1 As String
String1 = "this is the string content"
MsgBox Right(String1, 247)
MsgBox Left(String1, 714)
End Sub

Resulting VB.NET Code:

Private Sub method1()
Dim String1 As String = "this is the string content"
        MessageBox.Show(String1.Substring(String1.Length - Math.Min(String1.Length, 247)), Application.ProductName)
        MessageBox.Show(String1.Substring(0, Math.Min(String1.Length, 714)), Application.ProductName)
End Sub

Resulting C#.NET Code:

private void  method1(){

    string String1 = "this is the string content";
    MessageBox.Show(String1.Substring(String1.Length - Math.Min(String1.Length, 247)), Application.ProductName);
    MessageBox.Show(String1.Substring(0, Math.Min(String1.Length, 714)), Application.ProductName);
}
Talk To An Engineer