PC Review


Reply
Thread Tools Rate Thread

Can we pass a method to another class?

 
 
Tee
Guest
Posts: n/a
 
      9th Feb 2005
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


 
Reply With Quote
 
 
 
 
clu
Guest
Posts: n/a
 
      9th Feb 2005
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

 
Reply With Quote
 
Chad Z. Hower aka Kudzu
Guest
Posts: n/a
 
      9th Feb 2005
"Tee" <(E-Mail Removed)> wrote in news:evT#(E-Mail Removed):
> Can anyone tell me if it's possible to pass a method to another class?


Yes. You need to create a delagate. See delegates in the C# help.


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Make your ASP.NET applications run faster
http://www.atozed.com/IntraWeb/
 
Reply With Quote
 
clu
Guest
Posts: n/a
 
      9th Feb 2005
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

 
Reply With Quote
 
Adam
Guest
Posts: n/a
 
      9th Feb 2005
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

 
Reply With Quote
 
Tee
Guest
Posts: n/a
 
      9th Feb 2005
Thanks a lot, I will look into Delegate more.

"clu" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
Tim Jarvis
Guest
Posts: n/a
 
      10th Feb 2005
Tee wrote:

> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to pass data from a class data member to that class' display method? sherifffruitfly Microsoft C# .NET 1 17th Oct 2006 01:08 AM
How can I pass a different class and method name to a component? Stan Microsoft Dot NET Framework 0 8th May 2006 02:09 PM
How to pass a class to a method MrEd Microsoft C# .NET 8 7th Apr 2006 09:42 PM
Can we pass a method to another class? Tee Microsoft Dot NET 6 10th Feb 2005 06:12 AM
Pass Class as parameter in a method. Dion Heskett Microsoft C# .NET 1 4th Sep 2003 09:22 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:38 PM.