Article ID: 166928
Article Last Modified on 6/29/2004
Public MyProp As IntegerVisual Basic 4.0 passes the property by reference (ByRef).
MyClass.CLS
===========
Public MyProp As Integer
Public Sub MyMethod()
MyProp = 5 '// This has direct access to the class data
End Sub
===========
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: kbbug kbfix KB166928