Generic access of Class and Property values

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

Generic access of Class and Property values

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

by JADE News Administrator >> Thu, 29 Oct 1998 4:59:20 GMT

Hi
I want to create a generic method to take a class name and a property name and then say list the values of the property for all instances of the particular class -- say, on screen using the write function.

For example, you'd call it with statements like listProp("Customer", "custCode"); or listProp("Product", "prodCode"); and then the listProp method (created somewhere like off the Object class so it can be inherited from just about everywhere) would look something like this :

listProp(cls : Class; prp : Property);
begin

foreach obj in cls.instances do
write obj.Class.prp ///--- or do I have to use getPropertyValue perhaps ?????
endforeach;
end;


thoughts on a grey day ???.

SS

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

Re: Generic access of Class and Property values

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

by JADE News Administrator >> Thu, 29 Oct 1998 4:59:44 GMT

Hi,

Yes, you need to use getPropertyValue. This should do what you want:

listProp(cls : Class; propName : String);

vars
p : Property;
o : Object;
isReference : Boolean;
typeName : String;
begin

// See if the property is valid
p := cls.getProperties[propName];
if p = null then
write cls.name & "::" & propName & " doesn't exist";
return;
endif;

// Work out whether it's a reference or an attribute (could
// perform specific processing for references later on)
isReference := p.isKindOf(Reference);

// Get the type name of the property (could use this later to
// perform processing specific to a type)
typeName := p.getType.name;

foreach o in cls.instances do
if isReference then
// Could do something special for references
write "Ref=" & propName & ", Value=" &
o.getPropertyValue(propName).String;
else
// It's an attribute. Could perform processing specific to
// the type here, eg:
// if typeName = "String" then
// ...
// elseif typeName = "Binary" then
// ...
// and so on
write typeName & "=" & propName & ", Value=" &
o.getPropertyValue(propName).String;
endif;
endforeach;
end;

You could now call this with something like:

listProp(Customer, "name");

to list the name of all customers. Note that we don't enclose Customer in quotes (this would just give us a string, whereas using a class name directly gives us a reference to the class object which is what we want).

Hope this helps,
Dean.

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

Re: Generic access of Class and Property values

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

by JADE News Administrator >> Thu, 29 Oct 1998 5:00:18 GMT

It just occurred to me that you may have been wanting to pass a string with the class name so that you could enter it a text box or something. If so, you can get the class to pass to listProp as follows:

vars
cls : Class;
begin

// some code to show a form or something

cls := currentSchema.getClass(txbClassName.text);
if cls = null then
write "Class " & txbClassName.text & " not found";
else
listProp(cls, txbPropName.text);
endif;

// some more code
endif;

Cheers,
Dean.


Return to “Tips and Techniques”

Who is online

Users browsing this forum: No registered users and 2 guests