FIX: Cannot Change Variant Array in Class Module
ID: Q174004
|
The information in this article applies to:
-
Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, version 5.0
-
Microsoft Visual Basic Standard, Professional, and Enterprise Editions, 16-bit and 32-bit, for Windows, version 4.0
SYMPTOMS
In Visual Basic 4.0, an array stored in a variant class variable could be
changed from code external to the class. In Visual Basic 5.0, changing
values in the array will have no effect.
CAUSE
The behavior of Visual Basic 4.0 was incorrect. This has been corrected in
Visual Basic 5.0. In Visual Basic 4.0, storing an array in a variant
variable was commonly used as a workaround for the fact that arrays cannot
be declared as Public members of a class. However, this workaround is
neither necessary nor recommended. If the approach outlined in the next
section had been used, this problem would not have occurred regardless of
the version of Visual Basic in use.
RESOLUTION
Arrays cannot be declared as Public members of a class. The recommended
method of implementing an array as a member of a class is to declare the
array as Private, and create Property Let and Get methods to manage the
array. For example:
Private myarray() as String
Public Property Get marray(ByVal subscript As Integer) As String
marray = myarray(subscript)
End Property
Public Property Let marray(ByVal subscript As Integer, _
ByVal vNewValue As String)
On Error GoTo err_Array_Not_Initialized
If subscript > UBound(myarray) Then
ReDim Preserve myarray(subscript)
End If
myarray(subscript) = vNewValue
Exit Property
err_Array_Not_Initialized:
If Err.Number = 9 Then
ReDim myarray(1)
Resume
End If
End Property
STATUS
This problem has been corrected in Visual Basic 5.0.
MORE INFORMATION
Microsoft 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 functionality shown below, and acceptable in Visual
Basic 4.0, will need to be modified.
Steps to Reproduce Behavior That Was Previously Acceptable
- Create a new "Standard EXE" project in Visual Basic 5.0.
- Add a Class Module to the project.
- Add the following code to Class1:
Public aVariant As Variant
Private Sub Class_Initialize()
Dim anArray(2)
anArray(1) = "Hello"
anArray(2) = "World"
aVariant = anArray
End Sub
- Add the following code to the Click event of Form1:
Private Sub Form_Click()
Dim obj As New Class1
With obj
.aVariant(1) = "Goodbye"
.aVariant(2) = "Everyone"
Debug.Print .aVariant(1)
Debug.Print .aVariant(2)
End With
Set obj = Nothing
End Sub
- Run the project and click on Form1. Note that the values displayed in
the immediate window are "Hello" and "World", which are the original
values assigned to the array during the class initialization event.
- If desired, repeat steps 1 through 5 in Visual Basic 4.0. Note that the
values displayed in the immediate window are "Goodbye" and "Everyone."
Additional query words:
Keywords : kbVBp400 kbVBp500 kbGrpVB VB4WIN
Version : WINDOWS:4.0,5.0
Platform : WINDOWS
Issue type : kbbug