FIX: Cannot Change Variant Array in Class Module (174004)
The information in this article applies to:
- Microsoft Visual Basic Learning Edition for Windows 5.0
- Microsoft Visual Basic Professional Edition for Windows 5.0
- Microsoft Visual Basic Enterprise Edition for Windows 5.0
- Microsoft Visual Basic Standard Edition, 32-bit, for Windows 4.0
- Microsoft Visual Basic Professional Edition, 16-bit, for Windows 4.0
- Microsoft Visual Basic Professional Edition, 32-bit, for Windows 4.0
- Microsoft Visual Basic Enterprise Edition, 16-bit, for Windows 4.0
- Microsoft Visual Basic Enterprise Edition, 32-bit, for Windows 4.0
This article was previously published under Q174004 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.
Modification Type: | Minor | Last Reviewed: | 7/16/2004 |
---|
Keywords: | kbbug kbfix KB174004 |
---|
|