Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Remove item from VFP collection
Message
From
15/05/2005 09:08:11
 
 
To
15/05/2005 06:01:22
General information
Forum:
Visual FoxPro
Category:
Object Oriented Programming
Environment versions
Visual FoxPro:
VFP 9
OS:
Windows XP SP1
Network:
Windows 2000 Server
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01014330
Message ID:
01014337
Views:
20
>Hi,
>I have a collection that storing reference of empty objects. I want to remove those item which its cType = 'keyword'.
>
>I tried this code, but it doesn't work correctly. It only remove some matched item but not all.
>
FOR EACH loItem IN myCollection
>  IF loItem.cType = 'keyword'
>    myCollection.Remove(loItem.cKey)
>  ENDIF
>NEXT
>
>Currently, I use array to store all matched item key in FOR EACH loop, then only remove them one by one.
>
>Any other way to do this?
>
>Thank you

This is a bug.
The only solution is with a descending index loop
CLEAR
xx=null

createcoll()

xx.keysort = 0
* issue ( a bug for me )
? "forward loop bug ",'' at 20
FOR EACH occ IN xx && FOXOBJECT
	?? occ
	IF occ=5
		* internal index is 5
		xx.Remove(5)
		* now the collection is rolled down
		* the NEXT go to 6 ( = 7 now ) and skip 6
	ENDIF
NEXT
* simulation with index
createcoll()
? "forward index simulation",'' at 20
icc=1
DO WHILE icc<=xx.Count
	?? xx[m.icc]
	IF xx[m.icc]=5
		xx.Remove(5)
	ENDIF
	icc=icc+1
ENDDO

* for index 

createcoll()
? "backward index solution",'' at 20
FOR icc=xx.Count TO 1 STEP -1
	?? xx[m.icc]
	IF xx[m.icc]=5
		xx.Remove(5)
	ENDIF
NEXT

* for each try
createcoll()
? "backward loop bug",'' at 20
xx.keysort = 1
FOR EACH occ IN xx && FOXOBJECT
	?? occ
	IF occ=5
		xx.Remove(5)
	ENDIF
NEXT

PROCEDURE createcoll
xx = CREATEOBJECT("collection")
FOR k=1 TO 10
	xx.Add(k)
NEXT
Previous
Reply
Map
View

Click here to load this message in the networking platform