accessing objects

Forums for specific tips, techniques and example code
ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

accessing objects

Postby ConvertFromOldNGs » Fri Aug 07, 2009 2:48 pm

by beginner >> Tue, 20 Jan 2004 3:30:39 GMT

Hi,
I'm a newby in JADE. I would like to know how to access an object. For example ,I created a class People which has attributes sFirstname ,sLastname and sAge . Then I creaded method in JadeScripts to add people

addPeople();

vars
man : People;
begin
beginTransaction;

create man;
man.sFirstname:="Paul";
man.sLastname:="Frank";
man.sAge:="25";

create man;
man.sFirstname:="Sam";
man.sLastname:="Huang";
man.sAge:="30";

create man;
man.sFirstname:="Jimmy";
man.sLastname:="Wong";
man.sAge:="18";

commitTransaction;

end;

After compiled and excuted the script, three instances've been created. How can I delete some instance out of database.

This is my code to delete first instance:

begin
beginTransaction;

delete People.instances.last;

commitTransaction;
end;

Can someone explain how to delete instance beteween first and last instance?

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

Re: accessing objects

Postby ConvertFromOldNGs » Fri Aug 07, 2009 2:48 pm

by cnwdad1 >> Tue, 20 Jan 2004 4:02:22 GMT

Try taking a look at some of the Online Tutorials to help get you going:

http://www.jadeworld.com/Jade6/jade6_tutorials.htm

Basically, what you need is some root object. Create a class called "Root". On your Person class, add a reference to the Root class (say, "myRoot"). Create a MemberKeyDictionary subclass, say "PersonByLastNameDict", and give it a membership of Person, and add two keys, say "sLastname" and "sFirstname". On the Root class, add a reference to the PersonByLastNameDict (call it say "allPersons"), click the "Define Inverse" button, and select the "myRoot" reference of the Person class - you will probably have to do a reorg after this step. On your schema's Application subclass, create a reference to the Root class, say "myRoot".

On your schema's Application subclass, add a method called "initialize", and code something like:

initialize() updating;
vars
rootObj : Root;begin

rootObj := Root.firstInstance();
if rootObj = null then
beginTransaction;
create rootObj;
commitTransaction;
endif;
app.myRoot := rootObj;
end;

Because you've already created your instances of Person, you'll need to fix up your data, something like:

TestScript_FixPersons();
vars
rootObj : Root;
personObj : Person;begin

rootObj := Root.firstInstance();
if rootObj = null then
beginTransaction;
create rootObj;
commitTransaction;
endif;

beginTransaction;

foreach personObj in Person.instances do
personObj.myRoot := rootObj;
endforeach;

commitTransaction;
end;

Whenever your application starts, by default it will run "app.initialize". You can use a different name, but if you do, you'll need to specify it for your Application (refer to the Application Browser window, select the application, right-click, and select "Change"). Now, anywhere in your code, you can access the Person objects by referencing the singleton Root object, and iterating the collection of persons (i.e. allPersons). If you're just using a JadeScript, you can do something similar to the above initialize method to get a root object, for example:

TestScript_ListPersons();
vars
rootObj : Root;
personObj : Person;begin

rootObj := Root.firstInstance();
if rootObj = null then
beginTransaction;
create rootObj;
commitTransaction;
endif;

foreach personObj in rootObj.allPersons do
write personObj.sLastname & ", " & personObj.sFirstname;
endforeach;
end;

Finally, you should change your "addPeople" JadeScript to fetch a reference a reference to the singleton Root instance, something like above, and always set the "Person::myRoot" reference whenever you create an instance of Person, such as in the "TestScript_FixPersons" JadeScript example above.

Hope that helps a bit, but you really should at least give the Online Tutorials a go, or you could download them to run locally on your machine.

regards,
darrell.


Return to “Tips and Techniques”

Who is online

Users browsing this forum: No registered users and 19 guests

cron