Home All Groups Group Topic Archive Search About

return value, exit function

Author
25 Apr 2010 12:03 AM
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

Author
25 Apr 2010 12:45 AM
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.


--
Armin
Author
25 Apr 2010 2:23 AM
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 S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
  V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
Author
25 Apr 2010 6:42 PM
mp
Thanks to Armin and Herfried
mark

Show quoteHide quote
"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
news:uOCG84B5KHA.6052@TK2MSFTNGP02.phx.gbl...
> 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 S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
>  V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
Author
28 Apr 2010 6:08 PM
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
Show quoteHide quote
:-) Saga



"mp" <nospam@Thanks.com> wrote in message
news:%23zs2FcK5KHA.5476@TK2MSFTNGP06.phx.gbl...
> Thanks to Armin and Herfried
> mark
>
> "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
> news:uOCG84B5KHA.6052@TK2MSFTNGP02.phx.gbl...
>> 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 S   Herfried K. Wagner
>> M V P  <URL:http://dotnet.mvps.org/>
>>  V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
>
>