Properties of Object Passed ByVal Can Be ModifiedLast reviewed: May 16, 1996Article ID: Q138517 |
The information in this article applies to:
SUMMARYWhen you pass an object by value to a procedure, you can modify its properties in the procedure. Using ByVal with an object parameter affects how the object can be redefined in the procedure. If an object variable is passed to a procedure by using the ByVal keyword and the object parameter is set to a different object, the object variable still references the original object. Conversely, if an object variable is passed to a procedure by reference and the object parameter is set to a different object, the object variable references this different object. This article provides examples that highlight the functionality of the ByVal keyword.
MORE INFORMATIONConsider the following sample code where Class1 has one public text property called Description:
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 SubIn 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. If an object variable is passed to a procedure by using the ByVal keyword and the object parameter is set to a different object, the object variable still references the original object. Consider the following example code with the same definition of Class1:
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 SubIn 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. Conversely, if an object variable is passed to a procedure by reference and the object parameter is set to a different object, the object variable references this different object. The following example, using the same Class1, illustrates this:
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 SubIn 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." If you need to modify an object parameter's properties within a procedure without modifying the object passed, you need to create a copy of the object in the procedure. The following example of this method includes all the points previously discussed.
Step-by-Step Example
|
Additional reference words: 4.00 vb4win vb4all
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |