Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Managing dynamic objects

Status
Not open for further replies.

D-Ward

IS-IT--Management
Sep 6, 2022
27
1
3
GB
Afternoon,

I am creating Line objects, but having a stupid issue releasing them, so I can create another set later.

I create the objects no issue and name them lneGroup1, lneGroup2 ...... to remove them when I am done with them;

FOR EACH oObject IN thisform.ConArea.Objects
IF LEFT(oObject.Name, 8) = "lneGroup"
thisform.ConArea.RemoveObject(oObject.Name)
ENDIF
NEXT

It isnt removing all the objects that match the criteria in IF statement, if I have 4 it is only removing 2, so when I look to create them again with .NewObject later it tells me that I already have an object with that name.

I also tried it with;

FOR ox = 1 to thisform.ConArea.ControlCount

But this errors because ox ends up being greater than ControlCount which is reducing as I remove objects, am sure this is something stupid, and I have tried for a couple of hours and can not get a resovle.

There must be a simple way to remove all objects of a single type :-(

Darren
 
This means a Foreach loop will skip over objects when you remove something within the loop. And I can confirm this by a simple test form:

Code:
oForm = CreateObject("myForm")
oForm.listobjects()

Define Class myform as Form

Procedure Init()
   For i = 1 to 10 
      This.addobject("line"+Transform(i),"line")
   EndFor       
EndProc

Procedure removeobjects()
   For each loObject in This.Objects
       ? loObject.name
       This.RemoveObject(loObject.name)
   EndFor
EndProc 
 
EndDefine

It will list only lines with odd numbers. And they are the ones, that are removed. When line1 i removed from objects the loop does not continue with line2 but with line3. Likely because line2 now has become the first object and line3 the second, and the next item then is.

If you change to an index based approach the problem is exactly the same. But you can solve it starting from the last object:

Code:
Local i
For i = This.ControlCount To 1 Step -1
   If This.Objects(i).Name="line"
      ? This.Objects(i).Name
      This.RemoveObject(This.Objects(i).Name)
   Endif
Endfor

Chriss
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top