FIX: Cannot Change Variant Array in Class ModuleLast reviewed: September 18, 1997Article ID: Q174004 |
The information in this article applies to:
SYMPTOMSIn 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.
CAUSEThe 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.
RESOLUTIONArrays 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 STATUSThis problem has been corrected in Visual Basic 5.0.
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 functionality shown below, and acceptable in Visual Basic 4.0, will need to be modified.
Steps to Reproduce Behavior That Was Previously Acceptable
Keywords : VB4ALL VB4WIN vb5all Version : WINDOWS:4.0,5.0 Platform : WINDOWS Issue type : kbbug Solution Type : kbfix |
================================================================================
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |