PRB: "Object not a Collection" Trying to Read Array Element

Last reviewed: September 29, 1997
Article ID: Q129871
The information in this article applies to:
  • Standard, Professional, and Enterprise Editions of Microsoft Visual Basic, 16-bit and 32-bit, for Windows, version 4.0

SYMPTOMS

If a public member of a class of type variant is assigned an array, you get the following error message when trying to read an element of the array by directly indexing the variant member:

   Object not a Collection.

This happens only if the instance of the class object is late bound; that is, the instance is dimensioned "As Object."

RESOLUTION

  • Dimension the object instance as being of the exact type of the class, so that it is Early Bound.

    -or-

  • Index the variant member with another level of parenthesis. For example, in the "Steps to Reproduce Behavior" section of this article, use:

          Debug.Print x.arr()(0)
    

    Instead of:

          Debug.Print x.arr(0)
    

    This explicitly specifies that the variant holds an array.

STATUS

This behavior is by design.

MORE INFORMATION

Steps to Reproduce Behavior

  1. Start a new project in Visual Basic. Form1 is created by default.

  2. Choose Module from the Insert menu to add a new standard module (Module1)

  3. Add the following code to Module1:

          Sub main()
             Dim a(2) As Integer
             a(0) = 5
    
             Dim x As Object
             Set x = New Class1
    
             x.arr = a
             Debug.Print x.arr(0)
          End Sub
    
    

  4. Choose Class Module from the Insert menu to add a new class module (Class1).

  5. Add the following code to Class1:

          Public arr As Variant
    

  6. Press the F5 key to run the program. You will get the error message on the Debug.Print line.

Example Workaround

To work around the behavior, declare the object with a specific object type instead of Object. In Step 3 above, replace:

   Dim x As Object

with:

   Dim x As New Class1

Then change the Set statement into a comment. After making these changes, run the program again. You should see a value of 5 in the Debug Window.
Keywords          : IAPVBA VB4ALL VB4WIN vbwin GnrlVb kbprg
Technology        : kbvba
Version           : WINDOWS:4.0
Platform          : WINDOWS
Issue type        : kbprb


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


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 29, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.