method as an attribute

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

method as an attribute

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

by mike.price >> Wed, 18 Dec 2002 3:01:20 GMT

Can I store the name of a method as an attribute on an object and then retrieve this method to invoke it. if so how?????

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

Re: method as an attribute

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

by allistar >> Wed, 18 Dec 2002 3:21:23 GMT
Can I store the name of a method as an attribute on an object and then retrieve this method to invoke it. if so how?????

Sure. Add a property called "methodName" (or something) onto the class and to invoke it on an object do:

object.sendMsg(methodName);

Regards,
Allistar.

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

Re: method as an attribute

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

by mike.price >> Wed, 18 Dec 2002 19:34:21 GMT

Thanks very much.

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

Re: method as an attribute

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

by allistar >> Wed, 18 Dec 2002 20:07:03 GMT
Thanks very much.

You can also execute methods that take parameters using the:

object.sendMsgWithParams(methodName, paramaters);

method. This allows extra flexibility when standard inheritance won't do.
Regards,
Allistar.

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

Re: method as an attribute

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

by cdshearer >> Thu, 19 Dec 2002 0:50:40 GMT

Just a warning - sure you can do this, but be careful about performance. Obviously, JADE is going to have to look up the method from its name, and that won't be anywhere near as efficient as calling the method directly.

I wonder (out loud!) if you had a method you wanted to call a lot, whether it would be worth creating a transient method to call it from there - then it would be fairly efficient, provided you could absorb the cost of the compilation of the transient method in the first instance. Maybe this should be a homework assignment :-)

Craig

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

Re: method as an attribute

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

by allistar >> Thu, 19 Dec 2002 2:51:17 GMT

Something you can do in C (or C++) is to pass a pointer to a function around. This has it's advantages (although admittedly I hardly ever
use it). This is not possible in Jade (except for passing around the nazme of the function). As you say I wouldn't do it excessively as it could be slow.

We use this to allow users to run scripts. Instead of going:

if (scriptName = "blah") then
blah();
elseif (scriptName = "foo") then
foo();

etc. That's a pretty tedious way of coding it when you can go:

sendMsg(scriptName);

Regards,
Allistar.


Return to “Tips and Techniques”

Who is online

Users browsing this forum: No registered users and 3 guests