views:

407

answers:

4

Some comments on this SO question 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?

Edit: 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/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.

+4  A: 

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

So the compiler can't warn you about 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.

John Knoeller
I understand that part of the language. Then what is the purpose of the "Function/Operator with no return value" configuration in the Project Compiler Settings? That would seem to be a way to override that language behavior. You are saying it does not. So what does that compiler setting do?
Nick
@Nick: See GSerg's answer for an explanation of what that compiler setting does.
John Knoeller
A: 

That looks like a bug to me. I am able to reproduce it on my end in VS2008 SP1.

What is interesting is that it DOES work if it is a reference type, but does not if it is a value type.

rh
This is not a bug, but specified behaviour.
Ikke
Except they specified "Function/Operator with no return value" to be an error.
rh
Interesting about Object. I just reproduced that, and the Error says "Function 'TestWarning' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used."So it would seem that the compiler team was more concerned with situations where a Null Reference could occur, and not where invalid logic due to a default value might occur.
Nick
A value type (Structure) has a default value within its allowable range whereas a reference type (Class) being Nothing is obtuse enough to warrant the requirement. That's just my speculation.
John K
A: 

You aren't necessarily asking for a workaround but I'm just thinking out loud, you could make the return type of the function Nullable.

At runtime if the function reflects Nothing you know the programmer didn't explicitly assign a return value. Of course this only works for functions that don't naturally return Nothing. And it's inefficient in many ways.

John K
+3  A: 

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
GSerg
Best explanation... but still feels like a bug. Definitely wasn't the expected behavior based on that name of the compiler switch in the project settings. I would have expected Functions that return both Value Types and Reference Types to behave the same way, as they do in C#.
Nick
I agree. I personally believe this warning should not exist at all for Visual Basic. It sort of ruins one of the points of VB (that is, implicit initialization), and does it in an inconsistent and confusing way. Alas, the only thing you can do is stare at it long enough to make yourself believe it actually makes sense.
GSerg