Scott Scovell on Visual FoxPro

Thursday, December 01, 2005

AMEMBERS Gotcha

Again, here I hope to save a few fellow tax payers some time and possibly hardware. I have just spent two to three hours on this one and my head is still spinning. I'm joking about the hardware, I have a strict "three strikes and out" policy towards cruelty to IO devices.
 
I was unit testing some code I was writing for VFP Solution Explorer. The code was doing some type discovery of sorts on some user defined class instances based on the Empty class. In particular, I was using AMEMBERS() to place the properties of the passed object into an array.
 
I had similar code in a number of places and it was working in all but one. After a lengthy process of elimination, it turned out the only difference was the code that was failing was inside a FOR EACH...NEXT loop.
 
The following sample code demonstrates this
 

Local oCollection As Collection, oUser As Object, lnIndex As Integer

 

* Create an instance of a collection class so we can iterate

*     through the collection using For Each..Next and For..Next

oCollection = NewObject("Collection")

 

* Add a couple user defined objects to the collection

For lnIndex = 1 to 3

 

      * Create an user defined object with a couple of properties

      oUser = NewObject("Empty")

      AddProperty(oUser, "Property1", "Value1")

      AddProperty(oUser, "Property2", "Value2")

 

      * Add to collection

      oCollection.Add(oUser) 

Next

 

* Output the number of properties returned by AMembers() when

*     passed our user defined object in different ways

Clear

? "* First item of collection" + Chr(9), "Properties found: ", AMembers(laAtttributes, oCollection(1), 0)

 

* Using a For Each...Next loop

For Each oUser In oCollection

      ? "* For Each loop" + Chr(9) + Chr(9), "Properties found: ", AMembers(laAtttributes, oUser, 0)

      Exit

Next

 

* Using a For...Next loop

For lnIndex = 1 To oCollection.Count

      oUser = oCollection.Item(lnIndex)

      ? "* For Next loop" + Chr(9) + Chr(9), "Properties found: ", AMembers(laAtttributes, oUser, 0)

      Exit

Next

 
Notice, when inside a FOR EACH..NEXT loop, AMEMBERS() fails to retrieve any property information from the user defined object.
 
I favour using FOR EACH...NEXT loops over FOR...NEXT loops as I like to work with collections over arrays. But even when using arrays, Fox allows me to keep consistent and use FOR EACH...NEXT loops when iterating through arrays...perhaps not?
 
Powered By Qumana