getting an object from its instance id

For questions and postings not covered by the other forums
User avatar
ghosttie
Posts: 181
Joined: Sat Aug 15, 2009 1:25 am
Location: Atlanta, GA, USA
Contact:

getting an object from its instance id

Postby ghosttie » Tue Jul 09, 2013 9:37 am

If you know the class of an object and you have its instance id, is there a better way to get the object than this?

Code: Select all

user := (User.number.String & "." & id.String).asObject.User;
I expected a method like Class::getInstance(id : Decimal) : InstanceType; to exist but I can't see one.
I have a catapult. Give me all the money or I will fling an enormous rock at your head.

darrell
Posts: 6
Joined: Tue Apr 24, 2012 12:14 pm
Location: Melbourne

Re: getting an object from its instance id

Postby darrell » Tue Jul 09, 2013 4:26 pm

I'd just use String::asOid, e.g.

"2216.35".asOid().inspect();

murray
Posts: 144
Joined: Fri Aug 14, 2009 6:58 pm
Location: New Plymouth, New Zealand

Re: getting an object from its instance id

Postby murray » Tue Jul 09, 2013 4:27 pm

Hi ghosttie,
That's pretty much the same as what I have ended up doing in the past.
(Note: one shouldn't normally rely on object IDs - only in special cases).
I seem to recall looking into it and asking some folks at the time.
My code looks like yours, so I did not find a "better" way.
Murray (N.Z.)

User avatar
BeeJay
Posts: 312
Joined: Tue Jun 30, 2009 2:42 pm
Location: Christchurch, NZ

Re: getting an object from its instance id

Postby BeeJay » Tue Jul 09, 2013 5:05 pm

I'd just use String::asOid, e.g.

"2216.35".asOid().inspect();
Be careful of code like this if your system ever gets to the point where it needs to use the extended class number range for some of your classes. Instances of the extended class number range classes are not correctly handled by the "oid" versions of these methods and you should instead use the "object" versions of these methods. ie:

Use the "object" versions:
Object::getObjectStringForObject(someObject)
String::asObject()

Instead of the "oid" versions:
Object::getOidString()
Object::getOidStringForObject(someObject)
String::asOid()

Cheers,
BeeJay.

PS: Not that I'm in any way promoting the use of either version of these methods to turn a "string" back into an "object reference" or vice versa as part of regularly executed production code of course.... ;)


Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 13 guests