VB.Net Gotchas

VB.NET is a very user friendly language however I find its user friendliness to be quite confusing and error prone, especially when switching back and forth between C# and VB. Simple things like VB's handling of null or concatenation can lead to bugs if your not careful. For example:
 
   Sub Main()

        Console.WriteLine("The value of Nothing is also the value of the default type: " &
                         (Nothing = False))

        Console.WriteLine("The value of Nothing is also the value of the default type: " &
                          (Nothing = 0))

        Console.WriteLine("The value of Nothing is also the value of the default type: " &
                          (Nothing = String.Empty))

        Console.WriteLine("The & operator with two integers concats both: " & (3 & 4))

        Console.WriteLine("The + operator with two integers adds both: " & (3 + 4))

        Console.WriteLine("The + operator with two strings concats both: " & ("3" + "4"))

        Console.WriteLine("The & operator with a string and an integer concats both: " & 
                          ("3" & 4))

        Console.WriteLine("The + operator with a string and an integer adds both: " & 
                          ("3" + 4))

        Console.WriteLine("The + operator with an integer and string adds both: " & 
                          (4 + "3"))
        
        Console.WriteLine("The Length of a VB.NET array starts at 1 and not 0: " &
                          {"Dot", "Net", "Perls"}.Length)

        'Console.WriteLine("DirectCast can NOT convert types that are not inherited: " & 
        '               DirectCast(0, System.String))
        'System.IndexOutOfRangeException' 

        Console.WriteLine("CType can convert types that are not inherited: " &
                       CType(0, System.String))

        'Console.WriteLine("Nothing <> String: " & (Nothing = String.Empty))
        'System.IndexOutOfRangeException' 

        Console.WriteLine("Nothing is not string.Empty: " & (Nothing IsNot String.Empty))

        'Console.WriteLine("And operator always evaluates all conditions: " & 
        '                 (1 = 2 And 0 = String.Empty))
        'System.InvalidCastException

        Console.WriteLine("AndAlso operator evaluates left to right conditions as needed: " &
                          (1 = 2 AndAlso 0 = String.Empty))

        Console.WriteLine("The value of TestFunctionExitWithType is going to be the default" &
                          " return type: " & TestFunctionExitWithType())

        Console.WriteLine("The value of TestFunctionReturn is going to be Nothing: " &
                          TestFunctionReturn())

        Console.WriteLine("The value of TestFunctionExit is going to be Nothing: " &
                          TestFunctionExit())

        Console.WriteLine("Parenthesis are optional in Paremeterless methods: " & 
                          0.ToString)

        Console.ReadLine()

    End Sub

    Sub TestSubReturn()
        Return
    End Sub

    Sub TestSubExit()
        Exit Sub
    End Sub

    Function TestFunctionReturn()
        Return Nothing
    End Function

    'Compiler error: 'Return' statement in a Function, Get, or Operator must return a value.
    'Function TestFunctionReturn()
    '    Return
    'End Function

    Function TestFunctionExit()
        Exit Function
    End Function

    Function TestFunctionExitWithType() As Decimal
        Exit Function
    End Function

End Sub
Results:
The value of Nothing is also the value of the default type: True
The value of Nothing is also the value of the default type: True
The value of Nothing is also the value of the default type: True
The & operator with two integers concats both: 34
The + operator with two integers adds both: 7
The + operator with two strings concats both: 34
The & operator with a string and an integer concats both: 34
The + operator with a string and an integer adds both: 7
The + operator with an integer and string adds both: 7
The Length of a VB.NET array starts at 1 and not 0: 3
CType can convert types that are not inherited: 0
Nothing is not string.Empty: True
AndAlso operator evaluates left to right conditions as needed: False
The value of TestFunctionExitWithType is going to be the default return type: 0
The value of TestFunctionReturn is going to be Nothing: 
The value of TestFunctionExit is going to be Nothing: 
Parenthesis are optional in Paremeterless methods: 0

Comments

Popular posts from this blog

AutoItX4Java - Java AutoIt Bridge

The SQL Server and .Net equivalent of PHP and MySQL's SHA1 function

Automated web testing with Java, Selenium RC, LoggingSelenium, HtmlUnit and TestNG