Can we pass a method to another class?

  • Thread starter Thread starter Tee
  • Start date Start date
T

Tee

Hi,

Can anyone tell me if it's possible to pass a method to another class?

Example of what I would like to do:

class1:

public void MyMethod()
{
code here
}

class2:

public void CallThisMethod(something MethodName)
{
MethodName();
}

Then I will call this code from class1:
class2.CallThisMethod(MyMethod)

So is this possible in .NET? if yes, can anyone give me a proper example?


Thanks alot,
Tee
 
Sure you can.
The .NET way of doing this is by using delegates.
Delegates are the managed counterpart of the old-C function pointer ...
well, it's not exaclty a good sentence but I believe it makes thing
clearer !.

So what you have to do is:

1) Define a delegate. Delegates are types, the C# keyword "delegate" is
syntax sugar for defining your own type, inheriting from
MulticastDelegate and so on.

delegate void MyMethodDelegate();

--or--

delegate Boolean AnotherMethodWithArguments(Int32 i, ref String s, out
Object o);

2) Create an instance of the delegate type referring to your class1's
method

MyMethodDelegate d = new MyMethodDelegate(this.MyMethod);

3) Pass it to class2's method:

class2.CallThisMethod(d);

So your code will be re-written as follows:

class1:


// Nested inside class 1. This is just an option, it could be a
stand-alone type as well.
public delegate void MyMethodDelegate();

public void MyMethod()
{
code here



}


class2:

public void CallThisMethod(class1.MyMethodDelegate methodDelegate)
{
methodDelegate();


}


Then I will call this code from class1:
class2.CallThisMethod(new MyMethodDelegate(this.MyMethod));


Delegates offer much more than what I tried to explain in just a few
words, but this is more or less how they work !

For a good description about delegates, I suggest you to have a look at
the MSDN web site (msdn.microsoft.com) or at the excellent book by
Jeffrey Richter, Applied Microsoft .NET Framework Programming,
published by MSPress.

Best Regards.

Claudio Brotto
MCP, MCAD.NET
 
Sure you can.
The .NET way of doing this is by using delegates.
Delegates are the managed counterpart of the old-C function pointer ...
well, it's not exaclty a good sentence but I believe it makes thing
clearer !.

So what you have to do is:

1) Define a delegate. Delegates are types, the C# keyword "delegate" is
syntax sugar for defining your own type, inheriting from
MulticastDelegate and so on.

delegate void MyMethodDelegate();

--or--

delegate Boolean AnotherMethodWithArguments(Int32 i, ref String s, out
Object o);

2) Create an instance of the delegate type referring to your class1's
method

MyMethodDelegate d = new MyMethodDelegate(this.MyMethod);

3) Pass it to class2's method:

class2.CallThisMethod(d);

So your code will be re-written as follows:

class1:


// Nested inside class 1. This is just an option, it could be a
stand-alone type as well.
public delegate void MyMethodDelegate();

public void MyMethod()
{
code here



}


class2:

public void CallThisMethod(class1.MyMethodDelegate methodDelegate)
{
methodDelegate();


}


Then I will call this code from class1:
class2.CallThisMethod(new MyMethodDelegate(this.MyMethod));


Delegates offer much more than what I tried to explain in just a few
words, but this is more or less how they work !

For a good description about delegates, I suggest you to have a look at
the MSDN web site (msdn.microsoft.com) or at the excellent book by
Jeffrey Richter, Applied Microsoft .NET Framework Programming,
published by MSPress.

Best Regards.

Claudio Brotto
MCP, MCAD.NET
 
You can accomplish this using delegates. Declare the delegate with the
signature of the method that you'd like to pass around:

public delegate void MyMethodDelegate();

Then, write the actual method:

public void MyMethod() {
....code...
}

Then, you create an instance of the delegate:

MyMethodDelegate aDelegate = new MyMethodDelegate(MyMethod);

And the aDelegate object can be passed as a parameter:

Class2 class2Inst = new Class2();
class2Inst.CallThisMethod(aDelegate);

Where CallThisMethod is defined as:

public void CallThisMethod(MyMethodDelegate passedDelegate) {
passedDelegate();
}

Hope that helps.

Regards,

Adam
 
Tee said:
Hi,

Can anyone tell me if it's possible to pass a method to another class?

Example of what I would like to do:

class1:

public void MyMethod()
{
code here
}

class2:

public void CallThisMethod(something MethodName)
{
MethodName();
}

Then I will call this code from class1:
class2.CallThisMethod(MyMethod)

So is this possible in .NET? if yes, can anyone give me a proper
example?


Thanks alot,
Tee

Hi Tee,

You have had some good and helpful advice in here re delegates, but I
just thought that I would also offer up something else as well....

in your example, if you are just wanting to call a method of a class,
and its always going to be that class (or a derivative of that class)
the simplest solution would be to simply pass an instance of the class
itself and just call the method i.e.

public class Class1
{
Public void MyMethod()
{
// Code here
}

}

public class Class2
{
public void CallThisMethod(Class1 class1Instance)
{
class1Instance.MyMethod();
}
}

Now this might not be what you are after, if you are just after the
ability to call a method of a particular signature and don't care about
the class hierarchy, but in your particular example above, this
(passing the class) would be the simplest solution.

Cheers Tim.
 
Back
Top