FIX: Cannot Change Variant Array in Class Module

Last reviewed: September 18, 1997
Article 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

  1. Create a new "Standard EXE" project in Visual Basic 5.0.

  2. Add a Class Module to the project.

  3. 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
    
    

  4. 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
    
    

  5. 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.

  6. 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."
Keywords          : VB4ALL VB4WIN vb5all
Version           : WINDOWS:4.0,5.0
Platform          : WINDOWS
Issue type        : kbbug
Solution Type     : kbfix


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 18, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.