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 6 10776
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
"Tee" <th*@streamyx.com> wrote in news:ev*************@TK2MSFTNGP15.phx.gbl: 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/
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
Thanks a lot, I will look into Delegate more.
"clu" <cl************@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com... 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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Robert |
last post by:
Python doesn't know the class of a method when container not direct
class attribute:
>>> class X:
.... def f():pass
.... g=f
.... l=
....
>>> X.g
<unbound method X.f>
|
by: yoramo |
last post by:
hello
can I pass a static method as a parameter to a method?
if the answer is yes how do I do that ? how do I call the method ?
yoramo.
|
by: Jay Douglas |
last post by:
Hello, I am needing to pass a class object (this) by reference to a method
in a different class. When I do the following code I get the error (Cannot
pass '<this>' as a ref or out argument because...
|
by: z_learning_tester |
last post by:
I'm reading the MS press C# book and there seems to be a contradiction.
Please tell me which one is correct, 1 or 2.
Thanks!
Jeff
1. First it gives the code below saying that it prints 0 then...
|
by: KC Eric |
last post by:
Hi all,
I have a dll file, it has a class, say:
class Temp,
this class has a function which has a delegate as a parameter,
say:
public void Test(GameOverHandler _overHandler)
|
by: Tee |
last post by:
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()
{
|
by: darthghandi |
last post by:
I am creating a listener class that listens for incoming connections.
When I get an incoming connection, I want to signal an event to
happen and pass that connected socket to a different thread...
|
by: raylopez99 |
last post by:
I'm posting this fragment from another thread to frame the issue
clearer.
How to pass an object to a function/method call in C# that will
guarantee not to change the object?* In C++, as seen...
|
by: raylopez99 |
last post by:
Keywords: scope resolution, passing classes between parent and child
forms, parameter constructor method, normal constructor, default
constructor, forward reference, sharing classes between forms....
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |