Page 1 of 1
Correct format for referring to an object by ID
Posted: Wed Jun 09, 2010 7:36 pm
by phaworth
I have a need to refer to an object (stack, card, control) by it's ID property which is defined as being unique within the stack file. At the time I need to reference the ID, I don't know what type of object it is but I can't find a way to refer to the ID without prefixing it with "button" or "field" or something similar. I tried using "control" as a prefix but it caused a runtime error that it can't find the object.
Since the ID is supposed to be unique, I'm hoping there's a way to refer to it without knowing what type of object it relates to?
Pete
Re: Correct format for referring to an object by ID
Posted: Wed Jun 09, 2010 10:48 pm
by mwieder
"control" should do the job. You sure you got the syntax right?
put the name of control id 1004
update: of course, that won't work with "card" or "stack" since they're not controls...
Re: Correct format for referring to an object by ID
Posted: Wed Jun 09, 2010 11:18 pm
by phaworth
Ah yes, maybe I was accessing a card or stack at the time. I guess I could "control", "card", and "stack" in try blocks until I get one that works. Right now, the Ds are stored in a file that I read them from so perhaps I could also change the format of the file to include what type of object they are.
This is to do with my workaround for retaining custom property values between runs.
Pete
Re: Correct format for referring to an object by ID
Posted: Thu Jun 10, 2010 1:16 am
by mwieder
Ah... this sounds familiar. If you're trying to get a flat file representation of your stack format, I ended up with an xml format when I did this task. Putting it in xml allows you to represent the hierarchy unambiguously - you know whether you're dealing with a card or a group or a button or...
Code: Select all
<stack>
<card>
<name>"start"</name>
<button>
<name>"whatever"</name>
<id>1002</id>
</button>
</card>
<card>
</card>
</stack>
...and so on
Re: Correct format for referring to an object by ID
Posted: Thu Jun 10, 2010 5:09 pm
by dunbarx
This is intriguing. You have the ID, but not the object type. How do you have the ID?
Re: Correct format for referring to an object by ID
Posted: Thu Jun 10, 2010 5:19 pm
by mwieder
Not sure what the question is here... that's just sample xml off the top of my head. But I do have functions for describing a stack or any other object very explicitly, and then being able to recreate the object exactly from the xml description.
Do you mean how do you get the id of an object?
put the id of <whatever>
set the id of <whatever> to <someID>
Re: Correct format for referring to an object by ID
Posted: Thu Jun 10, 2010 5:37 pm
by phaworth
All goes back to the need to preserve custom property values across runs of a standalone app. I wrote a a couple of getProp handlers for custom properties that I need to preserve and put them in my main stack script so they get triggered by any put statements. That's because they're referred to all over my code and this provided an easy way to deal with them without finding and changing every reference to them.
The getProp handlers store the ID of the owning object, the custom property name and it's value in an external file. Initially, my plan was to process all the entries in this file when my app starts up to restore the values stored in it into their equivalent custom properties by referring to them by their ID, that's when I ran into this issue.
So it's not that I can't get the object type, it's just that I didn't store it in the file because I assumed that, since the ID is unique, it would provide me with a single item of data for updating the custom property values in my startup process. I can certainly change the format of the external file to include the object type to solve the problem.
I guess it seems to me that there should be some sort of syntax that lets you refer to an object by it's unique ID without having to identify it as a control, card or stack, maybe a keyword like "object" or something:
get the name of object ID 123456
Pete
Re: Correct format for referring to an object by ID
Posted: Thu Jun 10, 2010 5:46 pm
by mwieder
You *do* realize that the id of a stack isn't unique or constant, right?
Re: Correct format for referring to an object by ID
Posted: Thu Jun 10, 2010 9:47 pm
by dunbarx
Mark has a point, stacks are a bit different in that the id changes with changes in the number of objects. Might the "altID" which can remain constant, be a solution?
But your original thought of making the object references more complete from the getGo is probably best.
Re: Correct format for referring to an object by ID
Posted: Thu Jun 10, 2010 10:07 pm
by phaworth
Thanks for pointing out the non-uniqueness of stack IDs. Turns out that none of the custom properties I need to preserve are at the stack level so for this particular application, I don't need to worry about that. But I might in the future so I should probably figure out a way to deal with that.
I've added the object type to the external file and all seems to be working fine now.
Thanks for all the help,
Pete
Re: Correct format for referring to an object by ID
Posted: Thu Jun 10, 2010 10:15 pm
by mwieder
Stack names *are* unique within a stack/substack hierarchy, and they're unique in memory as well.
Re: Correct format for referring to an object by ID
Posted: Thu Jun 10, 2010 10:29 pm
by phaworth
Yep, I was thinking I would use the ID for everything except stacks and the name for stacks..
Pete