Question on Dynamic Conversion of Class Type

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

Question on Dynamic Conversion of Class Type

Postby ConvertFromOldNGs » Fri Aug 07, 2009 11:58 am

by Bizpoint >> Wed, 3 Jan 2001 19:40:40 GMT

Hi Jade Developers,
Got a questions that need advise from you all.

1)
The following codes allow the item instance to BankCd.code or User.staffID

Eg:
vars
item : Object ;
begin

if item.isKindOf('BankCd') then
write item.BankCd.code ; // where BankCd is a class
elseif item.isKindOf('User') then
write item.UserCd.code ; // where User is a class
endif;
end;


Is there anyway to pass in the class name dynamically and achieve the following result. Does Jade have the syntax to achieve the following result to reduce coding when there are a lot of code classes.


zPrintCode(dynamicClass : String);
vars
item : Object;begin


write item.dynamicClass.code;
// ^^^^^^^^^^
// is the passed in class name.

end;

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

Re: Question on Dynamic Conversion of Class Type

Postby ConvertFromOldNGs » Fri Aug 07, 2009 11:58 am

by Eric Peachey >> Thu, 4 Jan 2001 20:49:44 GMT

Hello Bizpoint,

I don't know if you can do dynamic type casting. However you could do something like:

zPrintCode( pObject : Object );
varsbegin

write pObject.getPropertyValue( "code" );
end;

This would achieve what you're trying to do in that example. The getPropertyValue (and setPropertyValue) methods aren't generally recommended as they allow you to violate encapsulation (i.e. you can read protected properties and set readonly properties) and are not particularly quick (apparently).

To also achieve the same thing you could define a method (say "getCode" ) at a suitable point in your class hierarchy (a common ancestor of both BankCd and UserCd classes). You could then reimplement that method in the appropriate subclasses to return whatever value is suitable for the class. If the common ancestor is an abstract class then getCode could be abstract at that level, and only implemented in the required classes (or make it a real method and return null).

Generally it's good OO practice to avoid using isKindOf. There's lots of discussion on the use of "isKindOf" type mechanisms. Some languages make it difficult to do "isKindOf" so that developers are reminded that there's probably a better way (See B. Meyer's OOSC book.)

All the best,

Eric in Dunedin

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

Re: Question on Dynamic Conversion of Class Type

Postby ConvertFromOldNGs » Fri Aug 07, 2009 11:58 am

by Paul Mathews >> Sun, 7 Jan 2001 8:15:41 GMT

Add a method on whaever class you like in your hierarchy, eg the ObjectClass eg Object.writeCode;
which is an empty method.

Hence for ANY instance you can always instance.writeCode;
Stuff happens only when this method is implemented where you want.


Paul Mathews
pem@cmsystemsgroup.com.au
Phone: [612] (99717384) Fax[612] (99711679)
(Dee Why,Sydney,Australia)

Please visit our homepage cmsystemsgroup.com.au.


Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 1 guest

cron