Article ID: 138517
Article Last Modified on 12/9/2003
Private Sub PassByVal(ByVal C As Class1)
C.Description = "Modified Value"
End Sub
Private Sub Form_Load()
Dim MyClass As Class1
Set MyClass = New Class1
MyClass.Description = "Original Value"
PassByVal MyClass
MsgBox MyClass.Description
End Sub
In this case, the message box displays "Modified Value" even though the
object is passed by value. You might expect that the ByVal would prevent
the procedure from modifying the property. However, when used with objects,
ByVal affects the procedure's ability to redefine the object.
Private Sub PassByValSet(ByVal C As Class1)
Dim A As Class1
Set A = New Class1
A.Description = "New Value"
Set C = A
End Sub
Private Sub Form_Load()
Dim MyClass As Class1
Set MyClass = New Class1
MyClass.Description = "Original Value"
PassByValSet MyClass
MsgBox MyClass.Description
End Sub
In this case, the message box displays "Original Value" because the
ByVal prevents the procedure from redefining the object with "Set C = A."
This is how ByVal works with objects.
Private Sub PassByRefSet(C As Class1)
Dim A As Class1
Set A = New Class1
A.Description = "New Value"
Set C = A
End Sub
Private Sub Form_Load()
Dim MyClass As Class1
Set MyClass = New Class1
MyClass.Description = "Original Value"
PassByRefSet MyClass
MsgBox MyClass.Description
End Sub
In this example, the message box displays "New Value" because the object is
passed by reference and the procedure is allowed to redefine it with "Set C
= A."
' This procedure modifies the description of the object
' passed in as expected
Private Sub PassByRef(C As Class1)
C.Description = "Modified Value"
End Sub
' You might expect that this procedure would not modify the
' Description property of the object that was passed in.
' However, it does modify it.
Private Sub PassByVal(ByVal C As Class1)
C.Description = "Modified Value"
End Sub
' This procedure redefines the original object to the new
' object with a new value for the description field.
Private Sub PassByRefAndSet(C As Class1)
Dim A As Class1
Set A = New Class1
A.Description = "New Value"
Set C = A
End Sub
' This procedure does not redefine the original object.
' However, within the scope of the procedure it is redefined.
Private Sub PassByValAndSet(ByVal C As Class1)
Dim A As Class1
Set A = New Class1
A.Description = "New Value"
Set C = A
End Sub
' This procedure shows how to redefine the object within the
' procedure to a copy of itself. Changes made in the locally
' redefined parameter will not be reflected back to the
' original object.
Private Sub PassByValAndCopy(ByVal C As Class1)
Set C = C.Copy
C.Description = "Modified Value"
End Sub
Private Sub Command1_Click()
Me.AutoRedraw = True
Print "Passing object by reference to modify property ";
Dim MyClass1 As Class1
Set MyClass1 = New Class1
MyClass1.Description = "Original Value"
PassByRef MyClass1
Print "results in: "; MyClass1.Description
Set MyClass1 = Nothing
Print "Passing object by value to modify property ";
Dim MyClass2 As Class1
Set MyClass2 = New Class1
MyClass2.Description = "Original Value"
PassByVal MyClass2
Print "results in: "; MyClass2.Description
Set MyClass2 = Nothing
Print "Passing object by reference to set object ";
Dim MyClass3 As Class1
Set MyClass3 = New Class1
MyClass3.Description = "Original Value"
PassByRefAndSet MyClass3
Print "results in: "; MyClass3.Description
Set MyClass3 = Nothing
Print "Passing object by value to set object ";
Dim MyClass4 As Class1
Set MyClass4 = New Class1
MyClass4.Description = "Original Value"
PassByValAndSet MyClass4
Print "results in: "; MyClass4.Description
Set MyClass4 = Nothing
Print "Passing object by value to set copy of object ";
Dim MyClass5 As Class1
Set MyClass5 = New Class1
MyClass5.Description = "Original Value"
PassByValAndCopy MyClass5
Print "results in: "; MyClass5.Description
Set MyClass5 = Nothing
End Sub
Public Description As String
' Create a member by member copy of Class1
Public Function Copy() As Class1
Dim Ret As Class1
Set Ret = New Class1
Ret.Description = Me.Description
Set Copy = Ret
End Function
Additional query words: 4.00 vb4win vb4all
Keywords: kbcode KB138517