Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Copying an Object - avoiding single object references
Message
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00365455
Message ID:
00365487
Views:
24
Rick,

For the XML stuff we are doing I'm using only SCATTER NAME created objects to mimic the hierarchical structure of an external DTD. I decided to not try to support arrays. For XML * tags I decided to use cursors instead. For this DTD:
< !ELEMENT trilliumtask (tasklist,ina)>
    < !ELEMENT tasklist (parse?,geocode?,match?)>
        < !ELEMENT parse (#PCDATA)>
        < !ELEMENT geocode (#PCDATA)>

        < !ELEMENT match (account,maxreturn)>
            < !ELEMENT account (#PCDATA)>
            < !ELEMENT maxreturn (#PCDATA)>

    < !ELEMENT ina (n?,f?,s?,g?,q?)>
        < !ELEMENT n (#PCDATA)>
        < !ELEMENT f (#PCDATA)>
        < !ELEMENT s (#PCDATA)>
        < !ELEMENT g (#PCDATA)>
        < !ELEMENT q (#PCDATA)>
I use VFP code:
TrilliumTask = this.CreateXMLDataObject( "tasklist ina" )

with TrilliumTask
   .TaskList = this.CreateXMLDataObject( "parse geocode match" )
   .TaskList.Match = this.CreateXMLDataObject( "account maxreturn" )
   .INA = this.CreateXMLDataObject( "n f s g q" )
   .INA.s = 0   && change to numeric type
   .INA.q = .f. && change to bool type
endwith
By default each leaf node assumes the datatype to be char. If it needs to be something else as ina.s above the initialize code changes the type. The code to build the XMLDataObject only runs once, and the object is used for subsequent XML transactions. There's a recursive routine to .null. out the leaf nodes.

I wrote a CloneXMLDataObject method to reuse an already defined subtree. It does a CreateXMLDataObject to a local and returns that object reference:
lparameters roObject

local loObject, i, n, laProperties[1]

loObject = this.CreateXMLDataObject( roObject._XMLTagOrder )

n = amembers( laProperties, roObject )
for i = 1 to n
   if ( at( "_XML", laProperties[i] ) = 0 )
      if ( type( "roObject." + laProperties[i] ) == "O" )
         store this.CloneXMLDataObject( eval( "roObject." + laProperties[i] ) ) to ("loObject." + laProperties[i])
      else
         store eval( "roObject." + laProperties[i] ) to ("loObject." + laProperties[i])
      endif
   endif
endfor

return loObject
Does this give you any ideas?

>I have a problem that maybe one of you has an idea on.
>
>This is inside of Web Connection for parsing XML into array properties which contain a set of objects. The logic to create and parse the objects into array properties works fine.
>
>But I'm unable to create these array objects in such a way that they are all unique - what happens at the moment is that a single reference is copied causing all items to point at the sae reference resulting in all of the elements having the same values.
>
>This doesn't work correctly, because of the fact that an object assigned to a variable is always a reference to another object.
>
>The problem here is that I can't instantiate the object - I have to use whatever the user passes in, which is a preinitialized object assigned in the first element of the array. In order Web Connection for WC to parse objects it needs to know the structure of the object which must be provided in the first element of the arary that is to be filled from XML.
>
>I have some code that does this with the array:
>
>lvValue = laArray[1] && This will contain the object 'blue print'
>lcType = VARTYPE(lcValue)
>
>IF lcType = 'O'
> DIMENSION laArray[lnRows]
> FOR x = 1 to lnRows
> laArray[x] = lvValue && assign all properties
> ENDFOR
>ENDIF
>lvValue = .F. && Delete the 'common' reference
>
>This works to initialize each of the array members with an object which can then be passed to parsexmltoobject.
>
>However, the problem above is: All of the array elements are basically pointing at the same object reference. Change one, you're changing them all.
>
>Anybody see a way of being able to create an array of objects that essentially contain a copy of the original object?
>
>The easy answers don't work in this scenario: Using the .Class property to create a new instance (some objects such SCATTER NAME don't have a Class member, nor would the properties contain the proper typed values, nor would nested subobjects or arrays be there).
>
>Anybody have any ideas?
df (was a 10 time MVP)

df FoxPro website
FoxPro Wiki site online, editable knowledgebase
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform