Harrris
asked on
Giving a method name as a parameter
Hello,
Mainly asking the following for java, but also interested how it applies to C# too
I have a "timer" class called Reminder which does some processing and run a method on another object every x seconds. (a new thread is created each time we create an instance of the class)
From within the main object of my application I create a new Reminder object, giving to its constructor a reference to the current object:
new Reminder(this)
the Reminder object will call a method ( for example doSomething() ) on the main object, every x seconds.
Is there any way to specify what method to be called on the main object from the Reminder object when
I create the Reminder object, instead of having to specify it withing the Reminder class ?
For example, when I create the Reminder object to give the method to be called as a parameter
in the constructor. Something like:
new Reminder(this, doSomething())
this way, I will be able to use the same Reminder class in many different situations within the application,
and be able to create many Reminder objects that will call different methods when the time is up, without having to re-write the Reminder class.
and b.t.w. is there a way (a keyword or something) to get a reference to the object from where the current object was created ? (so I don't have to give the "this" as a parameter to the Reminder constructor)
Mainly asking the following for java, but also interested how it applies to C# too
I have a "timer" class called Reminder which does some processing and run a method on another object every x seconds. (a new thread is created each time we create an instance of the class)
From within the main object of my application I create a new Reminder object, giving to its constructor a reference to the current object:
new Reminder(this)
the Reminder object will call a method ( for example doSomething() ) on the main object, every x seconds.
Is there any way to specify what method to be called on the main object from the Reminder object when
I create the Reminder object, instead of having to specify it withing the Reminder class ?
For example, when I create the Reminder object to give the method to be called as a parameter
in the constructor. Something like:
new Reminder(this, doSomething())
this way, I will be able to use the same Reminder class in many different situations within the application,
and be able to create many Reminder objects that will call different methods when the time is up, without having to re-write the Reminder class.
and b.t.w. is there a way (a keyword or something) to get a reference to the object from where the current object was created ? (so I don't have to give the "this" as a parameter to the Reminder constructor)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can use reflection and use a method name (as string) along with the pointer (the this) of the main object.
I do not think you can find out who called you (unless you want to hack into the call stack)
I do not think you can find out who called you (unless you want to hack into the call stack)
void CallMethod( Object theObject, String theMethod )
{
MethodInfo info = GetMethod( theObject, theMethod );
info.Invoke(theObject);
}
MethodInfo GetMethod( Type theObjectType, String theMethod )
{
Return theObjectType.GetMethod(theMethod, _
BindingFlags.FlattenHierarchy Or _
BindingFlags.Instance Or _
BindingFlags.Public Or _
BindingFlags.NonPublic);
}
Don't use reflection unless you don't know what method you want to call at compile time. You lose any type checking that the compiler does...
Either
1) Define an interface. Implement it in your calling object. Then Reminder calls a method on that interface
or
2) Pass in a delegate
So, something like
1):
interface IReminder { void RemindMe(); }
public class Caller : IReminder
{
public void RemindMe() { ... do stuff here }
public void startIt()
{
new Reminder( this ); <-- which is passing myself as 'IReminder'
}
class Reminder
{
public Reminder( IReminder toCall )
{
... when you're ready..
toCall.Remind();
}
}
or 2):
public delegate void RemindDel();
class Caller
{
void start()
{
new Reminder(new RemindDel(RemindMe) );
}
void RemindMe()
{ .. do stuff here }
}
class Reminder
{
public Reminder( RemindDel del )
{
.. then call with
del();
}
}
Either
1) Define an interface. Implement it in your calling object. Then Reminder calls a method on that interface
or
2) Pass in a delegate
So, something like
1):
interface IReminder { void RemindMe(); }
public class Caller : IReminder
{
public void RemindMe() { ... do stuff here }
public void startIt()
{
new Reminder( this ); <-- which is passing myself as 'IReminder'
}
class Reminder
{
public Reminder( IReminder toCall )
{
... when you're ready..
toCall.Remind();
}
}
or 2):
public delegate void RemindDel();
class Caller
{
void start()
{
new Reminder(new RemindDel(RemindMe) );
}
void RemindMe()
{ .. do stuff here }
}
class Reminder
{
public Reminder( RemindDel del )
{
.. then call with
del();
}
}
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/reflect/Method.html#invoke(java.lang.Object,%20java.lang.Object[])