VB.NET Gotchas for C# Devs

I have been coding in VB.NET on contract for just under a year now. And having done so, I have come up with a small list of 'Gotchas' that I have encountered. At times, these differences have cost me quite a bit of time in debugging, and if I can save someone else even a small amount of this pain, it will be worth it.

VB.NET does not throw a compiler error when you create a function without a return type. Can you tell me the return type of the method below?

    Public Function GetNumberOne()
        Dim num As Integer = 1
        Return num
    End Function

VB.NET is also content compiling a Function without a return statement. It isn't frequent that I forget to add a return statement, but it has happened.

    Public Function WithoutReturnStatement() As Integer
        Dim bool = False
        Dim s = "A String"
        Dim concat = String.Concat(s, bool)
 
    End Function

Another good idea: turn on Option Strict. If you don't, the runtime will attempt to do a lot of implicit conversions for you. I wrote some code where I thought that I could call GetBoolean() on a DataReader passing in a string representing the column name. It compiled fine. After running the application, I realized that I needed to pass in the columns index instead. Sure, if I would have slowed down, Intellisense would have told me different, but I was cocky

    Public Sub TakesNumberParam(ByVal value As Integer)
        Dim result = value + 1
    End Sub

Without option explicit on, I am able to write a test for the above method, that passes in a string that cannot be cast to an integer, and the project builds. At runtime, the code will throw an InvalidCastException.

    
Comments
Add Your Comment
Your email address will never be displayed anywhere.
Name:
Email:
Website:
Comment:
Type the word 'banana':
Blog Home
Recent Posts
Clearing the ClickOnce App Cache
VB.NET Gotchas for C# Devs
Dreamhost 1 year Special
Can I Suggest Bit Torrent?
Moq and VB.Net are Frenemies
Google Buzz - Impressions
O Rly? Intellisense requires XML + DLL
Self Documenting Code - FAIL
The 'F' in TFS is for 'Friction'
NDC 2009 Videos
Skills Test - FAIL!