委托问题,在调用时 [英] delegate problem, when invoking it
问题描述
大家好.
请帮帮我.
在工作期间,我想委托一个方法.
这是类Publisher
Hello all.
Please help me.
During my work, i want to delegate a method,.
This is class Publisher
public delegate void SaveAppontment(DateTime date, String s)
SaveAppointment sv =null;
if (sv != null)
{
sv.Invoke(DateTime.Now, "");
}
----------------------
我想从其他班级开始..
B类
========
在这里,我想在上面的委托中发送执行此工作的方法.
Publisher.SaveAppontment sd = method(date,s)//或其他什么东西,在这里我不在乎,我在乎发布者类,如果我使用的是正确的委托人!
----------------------
From my other Class, i want to do this..
CLASS B
========
Here i want to send the method that does the job in my delegate above.
Publisher.SaveAppontment sd = method(date, s)// or something,, i dont care here a lot, i care in the publisher class, if i using the delegtes right !!?
Is it possible ??
推荐答案
首先,您可以在开头使用sv = null,这意味着它将始终为null!这意味着将永远不会执行sv.Invoke(DateTime.Now, "");
.我对你在做什么感到有些困惑.这是使用委托的方法:
First of all you have sv= null at the beginning, which means it will always be null! which meanssv.Invoke(DateTime.Now, "");
will never be executed. I am a little confused about what you are doing. Here is how to use a delegate:
public delegate void SaveAppointmentDelegate(DateTime dt, string s);
public SaveAppointmentDelegate SaveAppointment;
public void FireSaveAppointmentEvent(DateTime dt, string s)
{
if (SaveAppointment != null) SaveAppointment(dt,s);
}
您可以通过以下方式将处理程序添加到SaveAppointment:
You add a handler to the SaveAppointment by the following:
SaveAppointment += SaveAppointmentEventHandler;
这篇关于委托问题,在调用时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!