[英]Why does my delegate not work when passed in as a parameter to a method?
I created a delegate and i want to use the delegate as a parameter in a method parameter list. 我创建了一个委托,我想使用委托作为方法参数列表中的参数。 I think want to call handler just like I did in the Main method which work perfectly.
我想要像在Main方法中那样完美地调用处理程序。
Question: How can I pass a delegate to a method? 问题:如何将委托传递给方法?
Code: 码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
public delegate void Del(string e);
Del handler = DelegateMethod;
public static void DelegateMethod(string message)
{
System.Console.WriteLine(message);
System.Console.ReadKey();
}
public void testDel(Del d) <--Write Here!!!
{
d.handler("33");
}
static void Main(string[] args)
{
// Instantiate the delegate.
// Del handler = DelegateMethod;
Program p = new Program();
p.handler("Hello World"); <--Like this example here (these work)
p.handler("DisneyLand");
p.handler("Cattle Wars");
testDel(p.handler("Cattle Wars");
}
}
}
Error List: 错误列表:
Error 1 The best overloaded method match for 'ConsoleApplication1.Program.testDel(ConsoleApplication1.Program.Del)' has some invalid arguments C:\Users\itpr13266\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 36 12 ConsoleApplication1
Error 2 Argument 1: cannot convert from 'void' to 'ConsoleApplication1.Program.Del' C:\Users\itpr13266\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 36 20 ConsoleApplication1
Error 3 ) expected C:\Users\itpr13266\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 36 44 ConsoleApplication1
Working Code: 工作守则:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
public delegate void Del(string e);
Del handler = DelegateMethod;
public static void DelegateMethod(string message)
{
System.Console.WriteLine(message);
System.Console.ReadKey();
}
public void testDel(Del d)
{
d.Invoke("L");
}
static void Main(string[] args)
{
// Instantiate the delegate.
// Del handler = DelegateMethod;
Program p = new Program();
p.handler("Hello World");
p.handler("DisneyLand");
p.handler("Cattle Wars");
p.testDel(p.handler);
}
}
}
In the line 在线
testDel(p.handler("Cattle Wars"));
p.handler
is invoked. 调用
p.handler
。 There is no longer a Del
to pass into testDel
. Del
不再传入testDel
。 Maybe you were looking for: 也许您在寻找:
testDel(p.handler);
This passes the Del
so that testDel
can invoke it. 这会传递
Del
以便testDel
可以调用它。
You have to pass the delegate into the method with out the "Cattle Wars" parameter 您必须将委托传递给方法而不使用“Cattle Wars”参数
testDel(p.handler);
Then you method should look like the following. 那么你的方法应该如下所示。
public void testDel(Del d)
{
d.Invoke("L");
}
I will have to say that delegates still seem strange to me. 我不得不说,代表们对我来说仍然很奇怪。 I know that they are just variables that are type safe and thread safe.
我知道它们只是类型安全且线程安全的变量。 You can use the variable just like calling the method name.
您可以像调用方法名一样使用变量。
public delegate void Del(string e);
Del handler = DelegateMethod;
In your example here your delegate can be set to any method that has a return type of void and takes one argument that is a string. 在您的示例中,您的委托可以设置为任何返回类型为void的方法,并且接受一个字符串参数。
Del handler = DelegateMethod;
The above is where you declare you delegate but you also can declare the delegate to any methods that excepts void and one parameter as a string. 上面是您声明委托的地方,但您也可以将委托声明为除了void和一个参数之外的任何方法作为字符串。
handler = BaseBall;
public void BaseBall(string a)
{
//code here
}
I would just keep reading you will learn as you go. 我会继续读你会随时学习。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.