Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Strange behaviour in deleting object
Message
From
08/08/2006 17:56:40
 
 
To
08/08/2006 12:00:01
General information
Forum:
Visual FoxPro
Category:
Other
Miscellaneous
Thread ID:
01143804
Message ID:
01143931
Views:
14
This message has been marked as the solution to the initial question of the thread.
>I built a container which I populate with a number of checkboxes depending on a certain table.
>To do that i removeobject all the checkboxes in the container, then addobjects again reading the table.
>
>I tried with
>
>for each oCheck in this.Mycontainer.objects
>    if lower(oCheck.baseclass) == 'checkbox'
>       this.removeobject(oCheck.name)
>    endif
>endfor
>
>
>but i found out that one of the objects was not removed
>so i'm using this code
>
>
>for i = this.mycontainer.controlcount to 1
>    if lower(this.mycontainer.controls(i).baseclass) == 'checkbox'
>       this.mycontainer.removeobject(this.mycontainer.controls(i).name)
>    endif
>endfor
>
>
>why that ?

FOR EACH uses an "hidden index",
when you remove an item with index n ,
the index of all the items with superior index becomes i-1,
( n+1 become n, n+2 become n+1 ),
so that the following item is jumped by the for loop.

But it is not equal to the FOR index=1 to Group.Count loop,
because it eval Group.Count on every cycle,
if it increase, continue the loop, otherwise exit.

look this:
CLEAR

Ocontainer = CREATEOBJECT("container")
Ocontainer.AddObject("object"+TRANSFORM(1),"custom")
M.DEND = DATETIME()+1
* infinite loop
FOR EACH oo IN Ocontainer.Objects FOXOBJECT
	? oo.Name
	XNAME=oo.Name
	Ocontainer.AddObject("object"+TRANSFORM(VAL(SUBSTR(m.xname,7))+1),"custom")
	IF DATETIME()>M.DEND
		EXIT
	ENDIF
NEXT

RELEASE Ocontainer

? "trytoremove"
Ocontainer = CREATEOBJECT("container")
Ocontainer.AddObject("object"+TRANSFORM(1),"custom")
M.DEND = DATETIME()+1
* 1 cycle only
FOR EACH oo IN Ocontainer.Objects FOXOBJECT
	? oo.Name
	XNAME=oo.Name
	Ocontainer.RemoveObject(XNAME)
	Ocontainer.AddObject("object"+TRANSFORM(VAL(SUBSTR(m.xname,7))+1),"custom")
	IF DATETIME()>M.DEND
		EXIT
	ENDIF
NEXT
Previous
Reply
Map
View

Click here to load this message in the networking platform