Exit function returns false?

In VB.NET for a boolean function, if you run the line "Exit function", it will return false?

+6
source share
4 answers

This is correct, with the caveat that in VB, the function name can also be a variable that is returned. If you previously set it to true, it will return true.


More fully, in VB.Net, if I have a boolean function Foo() defined like this:

 Public Function Foo() As Boolean '... 

... the body of this function has an implied variable, also called Foo , which corresponds to the return type of the function - Boolean in this case, but Object if the return type is omitted (you should use Option Strict , which requires a return type). This implied variable is initialized to use the default value for this type.

If you cannot Return a value from a function, whether through an Exit Function or just reaching the end, this implied variable is returned instead. Therefore, the Boolean function returns False if you Exit Function earlier without making any other changes, because this is the default value in the implied variable used with this function. But you could also set this variable to True if you want, and then Exit Function will return True .

Nowadays, people often don’t use an implied variable, but there are situations when it can save you a few lines of code without any cost for clarity.

+9
source share

Regardless of whether it does it or not (the compiler gives only a warning with a null reference), you should still explicitly return false, if only for readability.

+3
source share

Until you set this function to True before exiting

+2
source share

I always do "Return True" or "Return False" to exit the method instead of the exit statement.

+2
source share

All Articles