|
-
Apr 22nd, 2003, 01:09 AM
#1
Thread Starter
Addicted Member
How to differentiate ByVal and ByRef [RESOLVED]
Hi all,
I'm a bit confused by these by value and by reference. Can anyone show me an example of using this two? I can't differentiate them.
For example, is the following call byref or byval?
VB Code:
Private sub Form_Load
dim blnCorrect as boolean
call TrueOrFalse(blnCorrect)
if blnCorrect = True then
msgbox "True"
else
msgbox "False"
end if
End Sub
Private sub Passing(a as boolean)
.....
.....
a = Not a
End Sub
Please advice. 
Regards,
Jeremy
Last edited by jeremy_ckw; Apr 22nd, 2003 at 03:36 AM.
"If ignorance is bliss, that probably explain why I'm in a such a mess right now!!"
-
Apr 22nd, 2003, 01:37 AM
#2
Frenzied Member
ByRef will pass the pointer and ByRef will pass the actual contents
VB Code:
Private Sub Command1_Click()
Dim strTemp As String, strReturn As String
strTemp = "A variable"
strReturn = usingByVal(strTemp)
MsgBox "Orginal Variable: " & strTemp & vbCrLf & vbCrLf & _
"Returned Result: " & strReturn
strReturn = usingByRef(strTemp)
MsgBox "Orginal Variable: " & strTemp & vbCrLf & vbCrLf & _
"Returned Result: " & strReturn
strReturn = usingDefault(strTemp)
MsgBox "Orginal Variable: " & strTemp & vbCrLf & vbCrLf & _
"Returned Result: " & strReturn
'Using byRef the following function
strTemp = "Some Text"
strTemp = usingByRef(strTemp)
MsgBox strTemp
'Can be written as
strTemp = "Some Text"
usingByRef strTemp
MsgBox strTemp
End Sub
Private Function usingByVal(ByVal uVal As String) As String
'the contents of the variable is passed, the variable remains unchanged"
uVal = uVal & ". This variable has changed"
usingByVal = uVal
End Function
Private Function usingByRef(ByRef uVal As String) As String
'the pointer for the variable is passed. therefore
'the original variable also changes
uVal = uVal & ". This variable has changed"
usingByRef = uVal
End Function
Private Function usingDefault(uVal As String) As String
'Work out what this is!
uVal = uVal & ". This variable has changed"
usingDefault = uVal
End Function
HTH
"Brothers, you asked for it."
...Francisco Domingo Carlos Andres Sebastian D'Anconia
-
Apr 22nd, 2003, 01:38 AM
#3
Fanatic Member
ByRef (by reference)
A way of passing the address of an argument to a procedure instead of passing the value. This allows the procedure to access the actual variable. As a result, the variable's actual value can be changed by the procedure to which it is passed. Unless otherwise specified, arguments are passed by reference.
ByVal (by value)
A way of passing the value of an argument to a procedure instead of passing the address. This allows the procedure to access a copy of the variable. As a result, the variable's actual value can't be changed by the procedure to which it is passed.
From your example... if TrueOrFalse was a sub like so:
VB Code:
Public Sub TrueOrFalse(ByRef blnCorrect As Boolean)
Dim intCount As Integer
intCount = 2 + 2
If intCount = 4 Then
blnCorrect = True
Else
blnCorrect = False
End If
End Sub
then the blnCorrect variable in your Form_Load event would return MsgBox "True" message.
Better examples will probably come along here so hang on and checkem out too.
Hope this helps!
-
Apr 22nd, 2003, 03:34 AM
#4
Thread Starter
Addicted Member
Thanks!
VB Code:
Private Function usingDefault(uVal As String) As String
'Work out what this is!
uVal = uVal & ". This variable has changed"
usingDefault = uVal
End Function
If I'm not mistaken then the default should be by reference since it changed the original variable value. Thanks for making sure I understand! 
Thanks..both of you!
Regards,
Jeremy
"If ignorance is bliss, that probably explain why I'm in a such a mess right now!!"
-
Apr 22nd, 2003, 05:09 AM
#5
Frenzied Member
Ur Welcome. Cheers
"Brothers, you asked for it."
...Francisco Domingo Carlos Andres Sebastian D'Anconia
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|