return value, exit function

M

mp

If one has a class.
To use it some condition should be true.
So i give it an Init method of type boolean
being new to dotnet not sure how to code it?
Function Init() As Boolean
If ConditionNotMet Then
Exit Function <---would this leave the function before returning
false?
Return False
Else
....
Return True
End If
End Function
or
Function Init() As Boolean
If ConditionNotMet Then
Return False<---should you return value before leave the function?
Exit Function
Else
....
Return True
End If
End Function

also in VB6 the "return" syntax used to be
Init = True
am i correct in thinking it is now
Return True

I noticed i had used the old version somewhere and it still compiled...are
the two interchangable?
Thanks
mark
 
A

Armin Zingler

Am 25.04.2010 02:03, schrieb mp:
If one has a class.
To use it some condition should be true.
So i give it an Init method of type boolean
being new to dotnet not sure how to code it?
Function Init() As Boolean
If ConditionNotMet Then
Exit Function <---would this leave the function before returning
false?

It works like in VB6 (see below).
Return False

Isn't reached.
Else
....
Return True
End If
End Function
or
Function Init() As Boolean
If ConditionNotMet Then
Return False<---should you return value before leave the function?

It leaves the function returning False.
Exit Function

Isn't reached.
Else
....
Return True
End If
End Function

also in VB6 the "return" syntax used to be
Init = True
am i correct in thinking it is now
Return True

I noticed i had used the old version somewhere and it still compiled...are
the two interchangable?

It still works old style but I recommend "Return value" because it's clearer
where and which value is returned. If the function is exited without a return
statement, but either with the Exit Function statement or at End Function, the
last value assigned to 'Init', which is still an invisible, local variable,
is returned. If the return type is a value type, you don't get a compiler
warning (VB 2008) if you don't return a value for all code pathes. Untrue with
reference types.
 
H

Herfried K. Wagner [MVP]

Am 25.04.2010 02:03, schrieb mp:
If one has a class.
To use it some condition should be true.
So i give it an Init method of type boolean
being new to dotnet not sure how to code it?
Function Init() As Boolean
If ConditionNotMet Then
Exit Function<---would this leave the function before returning
false?
Return False

Yes, the function will be left returning 'False', because 'False' is the
default value for the 'Boolean' type and no value is assigned to 'Init'
before calling 'Exit Function'.
Function Init() As Boolean
If ConditionNotMet Then
Return False<---should you return value before leave the function?
Exit Function

Return will leave the function automatically, there is no need to call
'Exit Function'.
also in VB6 the "return" syntax used to be
Init = True

.... if followed by 'Exit Function'...
am i correct in thinking it is now
Return True

Yes, although the VB6-style is still supported.
I noticed i had used the old version somewhere and it still compiled...are
the two interchangable?

Yes.
 
M

mp

Thanks to Armin and Herfried
mark

Herfried K. Wagner said:
Am 25.04.2010 02:03, schrieb mp:

Yes, the function will be left returning 'False', because 'False' is the
default value for the 'Boolean' type and no value is assigned to 'Init'
before calling 'Exit Function'.


Return will leave the function automatically, there is no need to call
'Exit Function'.


... if followed by 'Exit Function'...


Yes, although the VB6-style is still supported.


Yes.
 
S

Saga

Thanks to Armin, Herfried and you Mp for asking.
I found this thread interesting and informative! I too
am coming from VB6 and was not 100% sure how
the "exit function" and "return" statements interacted
:) Saga



mp said:
Thanks to Armin and Herfried
mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top