Will Exit Function return a value?

  • Thread starter Thread starter Shawn McNiven
  • Start date Start date
S

Shawn McNiven

This is probably a silly question but what the heck:

I have the following function:

Public Function Validate() as boolean

Dim vValidate as boolean = true

If me.Name_English = "" then
vValidate = false
Exit Function
End if

If me.Name_French = "" Then
vValidate = false
Exit Function
End if

Return vValidate

End Function

Will the Exit Function return the value of vValidate? Or should I do:

If me.Name_English = "" Then
Return False
End if

Thanks.
Shawn McNiven
 
Shawn,

If your function never gets as far as the 'Return' statement then it will
simply just return the default value for the return type of your function
(in the case of a boolean it is False).

Gary
 
Like Gary mentions, just use Return...one less line of code and works better
and if you port your code to C#, it won't yell at you.
 
There's another horrible VB option you need to be aware of - in VB you
can assign to the function name and this is returned whenever you
happen to exit the function (C# has no equivalent abomination):

Public Function Validate() as boolean

If me.Name_English = "" then
Validate = false
Exit Function 'function will return false
Else
Validate = true
End if

'function returns true since that was the last assignment to the
function name
End Function
 
Hi Shawn,

Did you know that this can be done in VB.net in 3 rows (roughly typed)
If me.Name_English <> "" OrElse me.Name_French <> "" Then
Return true
End if

When I finaly found what was really the OrElse operator my opinion changed
and now I find it very powerful. (I see I get a little bit influenced by
Bill using words as powerful)

I hope this helps?

Cor
 
Thanks all for you help. I should have mentioned that my intent is to call Validate() to check the mandatory properties of my object prior to saving it to the database (I've also got validation on the front end, but want to have a last check prior to saving it). What I want to do is exit the function if any of the mandatory properties haven't been filled, as soon as it's discovered that validation has failed

So based on what was said, I should do the following right?

If me.Name_English = "" The
Return Fals
End i

and so on for all the properties I'm checking for

Cor: I liked the OrElse, but since I'm checking more than 2 field I'm worried it could start to look ugly

Thanks all
 
Back
Top