Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No warning in VB.NET when function has no return

Some comments on Stack Overflow question Why doesn't the C# compiler stop properties from referring to themselves? regarding warnings got me thinking about old issues that always goofed me up when I was writing more VB.NET code.

One of them was the fact that the compiler didn't warn if you declared a Function but never did an explicit Return statement or assign to the Function name. Running Visual Studio 2008, I just made a small experimental project, and it seems as though the behavior has never been fixed. I created a Visual Basic Console application, with the following code:

Module MainModule

    Sub Main()

        Dim test As Boolean = TestWarning()

    End Sub

    Function TestWarning() As Boolean

        Console.WriteLine("There is no Return Statement")

    End Function

End Module

I also went into the Project Settings and turned On Option Strict and Option Explicit. I also set the Warning Configurations so that "Function/Operator with no return value" was set to Error.

I compiled the project and got no warning, and no error on the TestWarning() Function. This seems like a great place to put a warning, because it will default to False, and you may have simply forgotten to do a return. C# will error without a return statement. I thought that VB.NET did the same thing with the "Function/Operator with no return value" configuration. Is this a bug, or is there something I'm missing?

Further Experimentation

Function TestWarning() As Boolean

    If DateTime.Now.DayOfWeek = DayOfWeek.Monday Then
        Return False
    Else
        Console.WriteLine("There is no Return Statement")
    End If

End Function

If I have an explicit Return in an If, and nothing in the Else, there is also no warning or error. It will simply take the default, even though you likely intended (via programming style) to have an explicit return. In this case, I explicitly returned False (which is the default for Boolean), so it's likely a hidden bug that I should have returned True in the Else.

like image 400
Nick Avatar asked Feb 25 '10 21:02

Nick


People also ask

What does it mean when a function doesnt return a value?

It means that that function does not return a value to the caller explicitly.

What does not return a value in Visual Basic function?

A Sub procedure does not return a value to the calling code. You call it explicitly with a stand-alone calling statement. You cannot call it by simply using its name within an expression.

Does not return a value on all code paths?

The error "Not all code paths return a value" occurs when some of the code paths in a function don't return a value. To solve the error, make sure to return a value from all code paths in the function or set noImplicitReturns to false in your tsconfig. json file.

What is IsNot nothing in VB net?

VB.NET 2.0 introduced a new operator, "IsNot", which is obviously the opposite of "Is". It is especially useful when testing for when something is not "Nothing". (I love the double-negative!) So the full new syntax when testing for Nothing is "value IsNot Nothing".


2 Answers

The warning will only let you know when a function is going to return Nothing by default.

You would get a warning if return value was of a reference type.

But your function has a return value of a value type, and those cannot be Nothing. Therefore, no warning.

This is because function name inside this very function acts as a result variable. You can return a value by assigning it to the function name instead of using Return. And all variables are initialized with default values, including the function-name variable. This is not the case in C, hence the different meaning of the warning.

Compare this to using variables before initializing them:

Dim x As Integer
CallFunction(x)  'No warning, x is implicitly and properly initialized to 0.

Dim y as Object
CallFunction(y)  'A warning: variable used before a value is assigned to it
like image 68
GSerg Avatar answered Oct 20 '22 01:10

GSerg


It's part of the BASIC language. All basic functions have a return type. If you do not specify a return type, then the type is assumed to be object.

So the compiler can't warn you about a missing return, because it doesn't know if you skipped the return intentionally or because you were taking advantage of the default return value feature of the language.

From this http://msdn.microsoft.com/en-us/library/sect4ck6(VS.80).aspx

if you use Exit Function without assigning a value to name, the procedure returns the default value for the data type specified in returntype. If returntype is not specified, the procedure returns Nothing, the default value for Object.

like image 39
John Knoeller Avatar answered Oct 19 '22 23:10

John Knoeller