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.