VB.NET Aaaarrrrggghhh - The missing return statement doesn’t give a compile error

Today I found another reason why i don’t like VB.NET

I forgot (commented out) the Return statement in a function (see below) to test different LINQ operators.
During running and later debugging my application I got strange LINQ runtime errors (1 result set is Nothing) …

Public Function PerformSearch(ByVal dc As myDataContext, _
                                  ByVal searchString As String, _
                                  ByVal memberId As Long) _
                    As IQueryable(Of SearchResult)

        searchString = GetFulltextSearchString(searchString)
        Dim q = From fulltextSearch In _
       '  -- SNIP SNIP -- removed LINQ code for blog

        ' Return q
    End Function

Figure: VB.NET function without a return statement


After a while I realized that the function doesn’t return nothing! But NO COMPILE ERROR! WTF???

Figure: BAD: No compile error

The compiler says: Warning “Function ‘PerformSearch’ doesn’t return a value on all code paths. A null reference exception could occur at run time when result is used.”

I realized that myself after a while :-| 


Figure: BAD: Visual Studio shows me a curly line underneath “End Function” – but then I have to mouse hover to actually see the problem


The weird thing is, that this is by convention (from MSDN) : If the function/procedure doesn’t assign a value to the function-name AND doesn’t use the “Return” statement the function returns the default value of the return type (=Nothing (null) in my case…)  :-(

My recommendations:

  • Always use the “Return” statement in VB.NET (when I don’t forget it ;-) ) instead of assigning a value to the “procedure name” and having “Exit Function”. Its just easier to read!
  • And to make the execution flow more easier to read, I prefer a “return” statement as last line of a method/function/procedure

See this BAD example

Public Function CalcMyValue(ByVal x As Integer, ByVal y As Integer) As Integer

        Dim returnValue As Integer
        If (x > 100 And y > 365) Then
            Return 1
            If (x > 100 And y > 365) Then
                returnValue = x * 1000
                Return -1
            End If
        End If
        returnValue = returnValue * 7
        Return returnValue

    End Function

Figure: Bad example with mix of return statements in the middle 

There is a Visual Studio option per Visual Basic project, to set the “compile warning” to a “compile error”
List with all warnings in VB on MSDN

Figure: Why is this an option? and why is the default a “Warning”?? WTF


Anonymous said...

know any way to set this as build error by default?

Peter Gfader said...

I couldn't find it out by now...
I was hoping it is a compiler setting in Tools|Options, but it is not...

I will update this post once I found out

Ritika Goswami said...

Thank you for your help really important information given keep sharing it

Post a Comment

Latest Posts

Popular Posts