in between firstInstance and lastInstance ?

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

in between firstInstance and lastInstance ?

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

by tim >> Sat, 16 Aug 2008 7:51:44 GMT

Hallo Jade Programmer

so Sorry if this is very stupid question.. coz I am newbie :-D
Is there short code to define/get value object in between firstInstance and lastInstance ( defined by the user or programmer - example I want get THIRD object value)

big thanks for your clue

tim

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

Re: in between firstInstance and lastInstance ?

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

by Tim M >> Sat, 16 Aug 2008 9:07:22 GMT

Hi from another tim and welcome to such a great programming language. You are doing it all wrong.

You should very rarely use the firstInstance, lastInstance, instances etc.. on the class. These are used in special occasions like debugging or when you can guarantee a certain object like a singleton but afaik this allows you to get access to an object when someone has locked it causing you to have an old edition of the object and all kinds of other errors.

You should be using collections to store lots of objects like array for example or member key dictionary when you want them to be order by key etc..

I suggest you read some tutorials, look at examples. There is some here and back in June I started a site at jadeprogramming.com. It has one sudoku example and a hello world tutorial at time of writing this but as I find the time I intend to expand it. I will be happy to help where I can.

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

Re: in between firstInstance and lastInstance ?

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

by Dean Cooper >> Sat, 16 Aug 2008 10:46:36 GMT

If you want to access objects by position, you can use an array. For example, if you have a Customer class with a public "name" property, this JadeScript will put ten Customer objects into an ObjectArray and then access two of them by position:

cust1();
vars
oa : ObjectArray;
c : Customer;
n : Integer;begin

create oa transient;
foreach n in 1 to 10 do
create c transient;
c.name := "Customer " & n.String;
oa.add(c);
endforeach;
write "The third customer is: " & oa[3].Customer.name;
write "The eigth customer is: " & oa[8].Customer.name;
epilog
oa.purge; // delete all the members of the array
delete oa; // delete the array
end;

To avoid the type cast when accessing elements of the array, you would define your own subclass of ObjectArray with a membership of Customer.

If you want to access objects by a key value, you can use a MemberKeyDictionary subclass. For example, if you have a MemberKeyDictionary subclass called CustomerNameDict, with a membership of Customer and a key of the Customer's "name" property, this JadeScript will add ten Customer objects to a CustomerNameDict and then access two of them by key value:

cust2();
vars
d : CustomerNameDict;
c : Customer;
n : Integer;begin

create d transient;
foreach n in 1 to 10 do
create c transient;
c.name := "Customer " & n.String;
d.add(c);
endforeach;
c := d.getAtKey("Customer 3");
write "The third customer is: " & c.name;
c := d["Customer 8"]; // equivalent syntax for getAtKey
write "The eigth customer is: " & c.name;
epilog
d.purge; // delete all the members of the dictionary
delete d; // delete the dictionary
end;

Dean.

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

Re: in between firstInstance and lastInstance ?

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

by tim >> Tue, 26 Aug 2008 12:22:28 GMT

Hello Dean

Thanks for your reply with very good example.

Scenarion in your example is you create Custumer data ( transient mode ) then add to ObjectArray.
After that data ( transient mode ) will be stay in ObjectArray.

I think, It still can not solve my problem. :-(
How to I get all Persistent Data -- that I already created before -- before I load to ObjectArray ?

Thank a lot for your Help

tim

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

Re: in between firstInstance and lastInstance ?

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

by Patwos >> Tue, 26 Aug 2008 21:26:20 GMT

Tim,

Generally you will have a singleton Root object class, often called Root in most systems, that has collections of your other objects on it.

eg: Root::allCustomers inversed to Customer::myRoot

Then, whenever you create a new Customer instance setting the Customer::myRoot reference automatically adds it to the Root::allCustomers collection.

Root::allCustomers would generally be a subclass of MemberKeyDictionary, ObjectSet, ObjectArray or ExtKeyDict with a membership of Customer. The last two options are less frequently used. If the inverse is solely for the purpose of referential integrity, using an ObjectSet is the best option. If the inverse is to allow getting at a specific instance via some key values, then using a MKD is the best option.

In most systems you'll often find there are multiple "allCustomer" MKD collections with different keys. eg:

Root::allCustomersBySurname
Root::allCustomersByCustId
etc.

As advised earlier by Tim M, there are a number of tutorials on the Jade Website that I'd recommend you look at as they cover this area of Jade. You may also want to have a quick look at the sample schema I attached to the Foreign Key thread in the jade_tech_design_architecture Newsgroup.

Don't be put off, as understanding how collections and inverses are put together can take some time to get your head around, particularly if you're used to working with tables in a relational database. The power of inverses becomes more apparent when you start having collections like Branch::allCustomers inversed to Customer::myBranch as these collections then have a subset of only those Customers for that branch.

Hope that helps,
Pat.

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

Re: in between firstInstance and lastInstance ?

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

by Dean Cooper >> Wed, 27 Aug 2008 0:26:25 GMT

The attached zip file contains a very simple Order and Order Line example system.

If you extract the zip, you'll see a 6.2.15 scm and ddb file that you can load into JADE to create an OrderEntry subschema of the RootSchema.

Copy the JPGs to a c:\temp\OrderEntry folder (the JadeScript below expects them to be in this location and will get an exception if they're not).

Once you've loaded the schemas, have a look at the JadeScript::createTheLot method in the OrderEntry schema. This deletes all existing data and then creates several persistent Product and Customer objects in the database. The very first thing it does (after the delete) is create a singleton Root object, as described by Patwos. If you look at the all* collections on the Root class, you will see that they each have a myRoot inverse reference on the related class. When the JadeScript sets the myRoot reference on an objects, JADE automatically adds that object to the related collection on the Root object.

Once you have a reference to the Root object, you can use the all* collections to access your data. That's what the simple GUI application in the OrderEntry schema does. If you look at OrderEntrySchema::initOrderEntry (the application's initialise method that is called when the application starts), you will see that it gets the first instance of the Root class and holds this in a myRoot reference on the transient app object (that every application has). You can define initialise and finalise methods for your application by selecting Browse | Applications, right clicking the application you want and selecting Change, then using the Initialize Method and Finalize Method combo boxes (enter a new method name or select an existing method).

If you look at all of the references to app.myRoot (select the myRoot property on the OrderEntrySchema class, right click and selected References), you can click on each method to see how it uses myRoot to access collections and retrieve data. For example, the OrderCreateForm::load method uses app.myRoot to iterate the allCustomers collection to load a combo box. The OrderLineCreateForm::load method iterates app.myRoot.allProducts to load a combo box.

From an OrderEntrySchema Class Browser window, click the green arrow in the toolbar and run the OrderEntryApp. Select Orders | Create Order. This has executed the OrderCreateForm::load method to load the Customer combo box with the data created by the JadeScript::createTheLot method. Enter an Order Number, select a Customer and then click New Line. This has executed the OrderLineCreateForm::load method to load the Product combo box with data. Select a Product, enter a Price and Quantity, then click OK. Click OK to create the Order. Now select Order | List Orders and you will see the Order you have just created. Select it to show the Order Line details. This has executed the OrderListForm::load and OrderListForm::tblOrders_mouseDown methods (which have in turn executed OrderListForm::refreshTheLot and OrderListForm::fillDetailTable).

Dean.
Attachments
5396_1.zip
(178.05 KiB) Downloaded 308 times


Return to “Tips and Techniques”

Who is online

Users browsing this forum: No registered users and 11 guests