No warning in VB.NET when function has no return

前端 未结 5 2013
梦如初夏
梦如初夏 2020-12-06 19:47

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

5条回答
  •  执笔经年
    2020-12-06 20:13

    I just wasted seven hours about the same problem. In my case (using Visual Studio 2012) the compilation ended with a compilation error:

    Error MSB3030: File "obj\x86 Debug\.exe" could not be copied, because the file was not found. (0, 0)

    (The text was translated from the German error message, and I hope it's translated properly.)

    Since that message didn't help, I reverted my project back to state where it compiled and finally tracked it down to something similar to the following:

    Function SomeFunction() as Boolean
      If  then 
        Exit Function
      end if
    end function
    

    If I replace the exit function with return false or if I insert a SomeFunction = false then the compiler error won't show up.

    So yes, as stated before, Visual Basic should default the return value to the default value of the return type, but it somehow crashed the compilation.

    I suspect this behaviour is hard to reproduce. My bottom line is: Set return values explicitly. It's clearer to the code and might save you seven hours of debugging.

提交回复
热议问题