Accessing Random Instances

For questions and postings not covered by the other forums
ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Accessing Random Instances

Postby ConvertFromOldNGs » Fri Aug 07, 2009 12:49 pm

by Mark >> Mon, 30 May 2005 3:58:37 GMT

Hi there,

I have a Class which I have created which I would like to be able to access instances randomly according to the order in which they were created.

At the moment I iterate through each item and count unitll I reach the item I want.

Thanks,

Mark

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Accessing Random Instances

Postby ConvertFromOldNGs » Fri Aug 07, 2009 12:49 pm

by allistar >> Mon, 30 May 2005 5:34:14 GMT

Are these instances stored in a collection of some sort? In your situation you could store them in a dictionary keyed by some sort of incementing integer, then a simple getAtKey would retrieve the object at a random position.

You could also store them in an array and access the nth one using the [] notation. The downside of an array is that they are expensive to remove instances from, especially will large collections.

Regards,
Allistar.
--
------------------------------------------------------------------
Allistar Melville
Software Developer, Analyst allistar@silvermoon.co.nz
Auckland, NEW ZEALAND

Silvermoon Software
Specialising in JADE development and consulting
Visit us at: http://www.silvermoon.co.nz
*NEW* Simple web access to Jade at: www.silvermoon.co.nz/jhp.html ------------------------------------------------------------------

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Accessing Random Instances

Postby ConvertFromOldNGs » Fri Aug 07, 2009 12:49 pm

by Mark >> Mon, 30 May 2005 5:56:05 GMT

Thanks for the prompt reply,

The collection I was hoping to use is of type VirtualColl... just simply the 'instances' pointer of the 'Class' class.

I have created my own class with a unique Integer for each item stored as a variable within each class. The way that I currently search is that I iterate amongst all instances of the object and seach for the ID number...


foreach member in Member.instances do
if memberID = member.getMemberID then
return member;
break;
endif;
endforeach;

I have tried to located the documentation for the VirtualColl class however couldn't find anything in the Help files to suggest that it have any support for random access. I don't mind using my iterative method for finding objects in the collection, however directly accessing them would be nice :)


Mark

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Accessing Random Instances

Postby ConvertFromOldNGs » Fri Aug 07, 2009 12:50 pm

by allistar >> Wed, 1 Jun 2005 3:29:53 GMT

Using the ".instances" and similar methods is not normally recommended for production code. You would be much better off creating a MemberKeyDictionary that stores all of the Member instances, keyed by the "memberID" property. Then you code above would look like this:

member := app.myRootObject.allMembersByID.getAtKey(memberID);

Regards,
Allistar.
--
------------------------------------------------------------------
Allistar Melville
Software Developer, Analyst allistar@silvermoon.co.nz
Auckland, NEW ZEALAND

Silvermoon Software
Specialising in JADE development and consulting
Visit us at: http://www.silvermoon.co.nz
*NEW* Simple web access to Jade at: www.silvermoon.co.nz/jhp.html ------------------------------------------------------------------

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Accessing Random Instances

Postby ConvertFromOldNGs » Fri Aug 07, 2009 12:50 pm

by Patwos >> Wed, 1 Jun 2005 3:46:18 GMT

Snap. How often it seems to happen that you're typing a reply, get interrupted and by the time you finish and then post your reply someone else has beaten you to the punch?!

Pat.

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Accessing Random Instances

Postby ConvertFromOldNGs » Fri Aug 07, 2009 12:50 pm

by Patwos >> Wed, 1 Jun 2005 3:45:01 GMT

Use of methods such as .instances is not generally recommended for production code. You would be much better off having a collection of Members keyed by memberID on some appropriate Root object class or similar and then use getAtKey, or the [ ] equivalent notation thereof.

eg: member := app.myRoot.allMembers[ memberID ];

Also, in your sample code, the break statement after the return statement is superfluous to requirements as it will never actually get executed.

Hope that helps,
Pat.

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Accessing Random Instances

Postby ConvertFromOldNGs » Fri Aug 07, 2009 12:50 pm

by Mark >> Wed, 1 Jun 2005 5:45:34 GMT

Thank you both :)


Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 21 guests