FIX: Public Properties of VB4 Class Are Passed by ReferenceLast reviewed: August 22, 1997Article ID: Q166928 |
The information in this article applies to:
SYMPTOMSIf you have a Visual Basic 4.0 class with a public property implemented like the following:
Public MyProp As IntegerVisual Basic 4.0 passes the property by reference (ByRef). However, if your property is implemented in Visual Basic 4.0 using property procedures (Property Get, Property Let, Property Set), it will be passed by value (ByVal).
STATUSThis problem has been fixed in Visual Basic 5.0. All properties are now passed by value (ByVal) for consistency. In addition, all code internal to the class still has direct access to member variable data:
MyClass.CLS
===========
Public MyProp As Integer
Public Sub MyMethod()
MyProp = 5 '// This has direct access to the class data
End Sub
===========
MORE INFORMATIONMicrosoft has acknowledged that this change in behavior may be an issue for some developers porting Visual Basic 4.0 code to Visual Basic 5.0. Code that relies on the ByRef functionality stated above will need to be modified. One possible way to modify the Visual Basic 4.0 for porting to Visual Basic 5.0 is shown below: Original Visual Basic 4.0 code:
MyClass.CLS
===========
Public MyProp As Integer
===========
MyBas.BAS
=========
Sub Compute(ByRef iTarget As Integer)
Dim iTemp as Integer
'// implementation code
iTarget = iTemp
End Sub
Sub Main()
Dim MyObject As MyClass
MyObject.MyProp = 97
Call Compute(MyObject.MyProp)
End Sub
=========
Modified Sub Main() for Visual Basic 5.0:
Sub Main()
Dim MyObject As MyClass
Dim iTtemp1 As Integer
Dim iTemp2 As Integer
MyObject.MyProp = 97
iTemp1 = MyObject.MyProp
iTemp2 = iTemp1
Call Compute(iTemp2)
If iTemp1 <> iTemp2 Then
MyObject.MyProp = iTemp2
iTemp1 = iTemp2
End If
End Sub
Exposing public variables is not a recommended programming technique. The
following demonstrates possible modifications to MyClass.CLS, illustrating
a data-hiding technique to preserve class member data:
MyClass.CLS
===========
Private m_MyProp As Integer
Public Property Let MyProp(NewValue As Integer)
'// implement validation code and data formatting
m_MyProp = NewValue
End Property
Public Property Get MyProp() As Integer
MyProp = m_MyProp
End Property
===========
Keywords : VB4WIN vb5all Version : 4.0 5.0 Platform : WINDOWS Issue type : kbbug Solution Type : kbfix |
================================================================================
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |