Returning 2 values

  • Thread starter Thread starter Bob Hollness
  • Start date Start date
Boy, my head is spinning further! I can't quite get my head around the
ByRef method though. How does it fit to the example below? E.g. If
False is returned, I also want to return the value of the variable
CookieLevel to show why it was returned False.

Your code would call into CheckMyValue, passing in the value 10 in the
Requested variable. This will be received in CheckMyValue into the
HowManyYouWant parameter. The function then sets CookieLevel to 5, compares
HowManyYouWant to CookieLevel and returns True or False. This doesn't return
the CookieLevel to the calling procedure.

To make this do what you want, try this:


Function RequestNumberOfCookiesInJar

Dim YesNo as Boolean
Dim Requested as Integer
Dim Available As Integer
Requested = 10
YesNo = CheckMyValue(Requested, Available)

If YesNo = True Then
MsgBox("Delicious!")
Else
MsgBox("There are only " & Available &" cookies in the jar")
End If

End Function


Function CheckRemainingCookies(ByVal HowManyYouWant as Integer, _
ByRef AvailableCookies As Integer)

Dim CookieLevel As Integer

CookieLevel = 5

'Set the ByRef parameter to the CookieLevel.
'This will be returned to the calling procedure
AvailableCookies = CookieLevel

If HowManyYouWant < CookieLevel Then
Return True
Else
Return False
End If

End Function


When you run this, the CheckRemainingCookies function will receive a
reference to your "Available" variable in its "AvailableCookies" parameter
variable. Any changes it makes to this will be reflected in the "Available"
variable in the RequestNumberOfCookiesInJar function. This is the effect
that the ByRef keyword has -- if this were ByVal (like the HowManyYouWant
parameter) then changes made within CheckRemainingCookies would have no
effect on the variable in the calling function.

Does that make it any clearer?
 
Oenone said:
Your code would call into CheckMyValue, passing in the value 10 in the
Requested variable. This will be received in CheckMyValue into the
HowManyYouWant parameter. The function then sets CookieLevel to 5,
compares HowManyYouWant to CookieLevel and returns True or False. This
doesn't return the CookieLevel to the calling procedure.

To make this do what you want, try this:


Function RequestNumberOfCookiesInJar

Dim YesNo as Boolean
Dim Requested as Integer
Dim Available As Integer
Requested = 10
YesNo = CheckMyValue(Requested, Available)

If YesNo = True Then
MsgBox("Delicious!")
Else
MsgBox("There are only " & Available &" cookies in the jar")
End If

End Function


Function CheckRemainingCookies(ByVal HowManyYouWant as Integer, _
ByRef AvailableCookies As Integer)

Dim CookieLevel As Integer

CookieLevel = 5

'Set the ByRef parameter to the CookieLevel.
'This will be returned to the calling procedure
AvailableCookies = CookieLevel

If HowManyYouWant < CookieLevel Then
Return True
Else
Return False
End If

End Function


When you run this, the CheckRemainingCookies function will receive a
reference to your "Available" variable in its "AvailableCookies" parameter
variable. Any changes it makes to this will be reflected in the
"Available" variable in the RequestNumberOfCookiesInJar function. This is
the effect that the ByRef keyword has -- if this were ByVal (like the
HowManyYouWant parameter) then changes made within CheckRemainingCookies
would have no effect on the variable in the calling function.

Does that make it any clearer?

Thank you. This was the missing part. All works great.
 
Bob,

Beside all theories,

Did you know that the value -1 is often used for false in VBNet?

Just a thought,

Cor
 
Yes, it is just habit to use True or False. I think it goes back to the
days i used it on Basic V on my Archimedes!!!! Doesn't matter though does
it? and it makes the code easier to read (IMO)
 

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

Back
Top