O
Oenone
Boy, my head is spinning further! I can't quite get my head around the
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?
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?