I am trying to create a collection of sets where every set is a collection of elements. But having a technical glitch. My Program is as follows:
Dim app As Object
Set app = GetObject(, "femap.model")
Dim pr As Object
Set pr = app.feProp
Dim elcol As Object
Set elcol = app.feSet
Dim elemcol As New Collection
Set elemcol = Nothing
For i = 2 To 4
tmp = elcol.AddRule(i, FGD_ELEM_BYPROP)
For Each elcol In elemcol
At the initial debug.print, the number of elements in a set prints right.But when i try to extract each elcol set from the collection elemcol, the result turns out to be 0.
What mistake am i doing.
Could someone help me please.
Solved! Go to Solution.
Within your for loop, you are referencing the same “elcol” object each time. In other words, you have three references stored in your collection that are all pointing to the same object.
Whenever you call the elcol.Clear method, it clears the single “elcol” object stored in memory. When you loop over the collection you have three references that are retrieving the single object three times. Since you cleared that object at the end of the pervious loop, it shows that it has nothing in it three times.
The way around this is to initialize a new set object at the start of the loop and then dereference it at the end of the loop. Doing this creates a new object at the beginning of each loop, and by dereferencing it at the end of the loop, you know that you won’t get a reference pointing to the pervious object.
Sub abc() Dim app As Object Set app = GetObject(, "femap.model") Dim pr As Object Set pr = app.feProp Dim elcol As Object Dim elemcol As New Collection 'Set elemcol = Nothing Dim i As Long Dim tmp As Long For i = 2 To 4 Set elcol = app.feSet tmp = elcol.AddRule(i, FGD_ELEM_BYPROP) Debug.Print elcol.count elemcol.Add elcol Set elcol = Nothing 'elcol.Clear Next i For Each elcol In elemcol Debug.Print elcol.count 'elcol.Clear Next End Sub