Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Bug? Calling methods in object returned by access method
Message
 
To
20/02/2003 15:26:24
General information
Forum:
Visual FoxPro
Category:
Other
Miscellaneous
Thread ID:
00755131
Message ID:
00755862
Views:
12
Hi Cameron,

thanks for your fast reply on the issue!

>Forgive me if I am missing something but I don't see why much of this code should work at all.

No problem, I will try to clarify what I'm doing. It's not just a theoretical example, it's a real world one. Maybe the code was a little confusing. The basic idea is to have a reference to a collection object stored in another collection and directly call a method of the inner collection.
*-- With VFP8's native collection class it would look like so:

loCollection = CREATEOBJECT("Collection")
loCollRefToBeStoredInCollection = CREATEOBJECT("Collection")

loCollection.Add(loCollRefToBeStoredInCollection)

*-- Here we are adding an item to the collection object of which
*-- a reference is stored in the first item of loCollection
loCollection.Item(1).Add("Some value")
The following code demonstrates for what I really use the assign method. It's simply a wrapper for the internal array. I ask for an array property (Items) value and simply return an array item from the internal array. Since the returned array element is an object reference, I can directly call methods in the returned object just like I would be able to call an object's methods when it is stored in a public array property without an assign method. I hope I was able to describe this properly, so you can follow the thoughts behind the code.
*--------------------------------------------------------------
*--
*-- Visual FoxPro 08.00.0000.2521 for Windows
*--
*-- Works fine in Visual FoxPro 07.00.0000.9465 for Windows
*--
*-- Demonstrates possible access method bug when directly
*-- calling a method in the returned object like so:
*--
*-- THIS.uProp(1).Add("1")
*--
*-- The access method gets .F. instead of 1 as it's
*-- parameter value. For workarounds see below.
*--
*-- Submitted by armin.neudert@ansta.de
*-- 02-19-2003 08:50 pm
*-- Version 2
*--------------------------------------------------------------

LOCAL loCollection, loCollRefToBeStoredInCollection

loCollection = CREATEOBJECT("myCollection")
loCollRefToBeStoredInCollection = CREATEOBJECT("myCollection")

loCollection.Add(loCollRefToBeStoredInCollection)

*-- Here we are adding an item to the collection object of which
*-- a reference is stored in the first item of loCollection

*-- Error in VFP8!
loCollection.Item(1).Add("Some value")

*-- No error!
LOCAL loTemp
loTemp = loCollection.Item(1)
loTemp.Add("Some value")

RETURN



DEFINE CLASS myCollection AS Session 

	*-- This array is only there so we can have
	*-- an access method we can use to wrap
	*-- access to the internal array
	DIMENSION Item[1]
	
	*-- This array is the internal one that
	*-- holds our items. No direct access
	*-- from the outside allowed (encapsulation!)
	PROTECTED aItems[1]

	PROCEDURE Add
		LPARAMETERS tluItem
		
		IF THIS.aItems[1] <> .F.
			DIMENSION THIS.aItems[THIS.ALEN(aItems)+1]
		ENDIF
		
		THIS.aItems[ALEN(THIS.aItems)] = tluItem

	ENDPROC

	PROCEDURE Item_Access
		LPARAMETERS tlnIndex

		MESSAGEBOX( "Index passed to access method: " + TRANSFORM(tlnIndex) )
		
		RETURN THIS.aItems[tlnIndex]
	
	ENDPROC

ENDDEFINE
>Are you using a documented feature that I am not aware or is this code taking advantage of an anomaly in VFP?

IMO it's an absolutely "legal" way of using access methods. There should be some more people out there doing this the same way ...

Regards,
Armin

Armin Neudert
Regional Director (Stuttgart) of German FoxPro User Group dFPUG

MCP for Visual FoxPro

Expert/Editor of the VFP section in the German Codezone community portal
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform