0

I need to create a collection with class objects. These class objects should in turn contain a collection.

So the data structure from top to bottom would be:

  • Collection "col_A" contains class objects "cls_A"
  • Class objects "cls_A" contain an integer "inta" and a collection "col_B"
  • Collection "col_B" contains class objects "cls_B"
  • Class objects "cls_B" contain an integer "intb" and a double "dbl"

Everything works fine until I try to add the "cls_A" object to the top-most collection. Then I get the following error:

Run-time error '91':
Object variable or With block variable not set

My code is the following:

Class Module cls_A

Public inta As Integer
Public col_B As Collection

Class Module cls_B

Public intb As Integer
Public dbl As Double

Main Module

Sub main()

Dim objB As cls_B
Dim col_B As New Collection

'Populate the col_B collection
'(with one element for simplicity)
Set objB = New cls_B
objB.intb = 5
objB.dbl = 5.15
col_B.Add objB

'Populate the col_A collection
Dim objA As cls_A
Dim col_A As Collection

Set objA = New cls_A
objA.inta = 2
Set objA.col_B = col_B
'Up to here there is no problem,
'objA is correctly created

col_A.Add objA  'Here I get the error

End Sub
David R
  • 55
  • 7

1 Answers1

1

Dim col_A As Collection doesn't create col_A -- it merely declares it. The line that throws the error is attempting to use this non-existent object.

Try

Dim col_A As New Collection

Alternatively, Set it after you declare it but before you use it.

John Coleman
  • 51,337
  • 7
  • 54
  • 119