Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Exit Function return false?

Tags:

vb.net

In VB.NET on a boolean function if you run an Exit Function line will it return false?

like image 557
MetaGuru Avatar asked Apr 03 '09 14:04

MetaGuru


Video Answer


4 Answers

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.

like image 58
Joel Coehoorn Avatar answered Oct 20 '22 15:10

Joel Coehoorn


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

like image 20
hmcclungiii Avatar answered Oct 20 '22 16:10

hmcclungiii


As long as you have not set that function to True before you exit

like image 43
kevchadders Avatar answered Oct 20 '22 16:10

kevchadders


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

like image 2
Rick Rat Avatar answered Oct 20 '22 14:10

Rick Rat