In VB.NET on a boolean function if you run an Exit Function line will it return false?
That is correct, with the caveat that in VB the function name can also be a variable that is returned. If you've previously set that to true, it will return true.
More completely, in VB.Net, if I have a boolean function Foo()
defined like so:
Public Function Foo() As Boolean
'...
...the body of that function has an implied variable also named Foo
that matches the return type of the function — Boolean
in this case, but Object
if the return type is omitted (you should be using Option Strict
, which requires a return type). This implied variable is initialized to use the default value for that type.
If you fail to Return
a value from the function, whether via Exit Function
or simply by reaching the end, this implied variable is returned instead. Therefore, a Boolean
function will return False
if you Exit Function
early without making other changes, because that is the default value in the implied variable used with the function. But you could also set that variable to True
first if you wanted, and then Exit Function
would cause it to return True
instead.
These days people don't often use the implied variable, but there are situations where it can save you a few lines of code without costing anything in clarity.
Regardless if it does or not (the compiler gives a null-reference warning only), you should still explicitly return false, if only for readability.
As long as you have not set that function to True before you exit
I always do "Return True" or "Return False" to exit a method instead of the exit statement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With