C# 如何防止空委托?
我在stackoverflow和其他网站上搜索了两天,试图找出为什么我得到一个空委托。我讨厌发布一个已经回答过的问题,但我对这个问题迷茫了。请原谅我,如果这已经在这个网站上的某个地方 我的目标是将一些值从一种形式传递到另一种形式。但是delPassData总是空的 我的主窗体是Form1,我试图从另一个窗体formNewThread向它发送值。以下是formNewThread中的相关代码:C# 如何防止空委托?,c#,C#,我在stackoverflow和其他网站上搜索了两天,试图找出为什么我得到一个空委托。我讨厌发布一个已经回答过的问题,但我对这个问题迷茫了。请原谅我,如果这已经在这个网站上的某个地方 我的目标是将一些值从一种形式传递到另一种形式。但是delPassData总是空的 我的主窗体是Form1,我试图从另一个窗体formNewThread向它发送值。以下是formNewThread中的相关代码: public partial class formNewThread : Form { publi
public partial class formNewThread : Form
{
public delegate void delPassDataEventHandler(string where, string newThd); //declare delegate to communicate with Form1
public event delPassDataEventHandler delPassData;
private string newThread;
private string whereUsed;
public formNewThread(string whereTo, ComboBox cb)
{
InitializeComponent();
whereUsed = whereTo;
... removed some code here
}
public formNewThread()
{
InitializeComponent();
}
private void buttonPublish_Click(object sender, EventArgs e)
{
newThread = textNewThread.Text;
... removed code here
if (delPassData != null)
delPassData(whereUsed, newThread);
this.Close();
}
表1中的相关代码为:
public Form1()
{
try
{
InitializeComponent();
frmNewThd.delPassData += new formNewThread.delPassDataEventHandler(frmNewThd_delPassData);
newCsg.delPassData += new NewCsg.delPassDataEventHandler(newCsg_delPassData);
... removed code
}
void frmNewThd_delPassData(string where, string newThd) //this routine communicates with with delegate in form formNewThread
{
if (where == "Lnr1")
{
comboLinerThread.Text = newThd;
}
else if (where == "Body")
{
comboBodyThdDown.Text = newThd;
}
else if (where == "SetTool")
{
comboSetToolThd.Text = newThd;
}
}
下面是我测试的简单模型,用于实现您在上面第一个程序中看到的代理
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace SendDataWithDelegate
{
public partial class Form1 : Form
{
private Form2 frm2 = new Form2();
private Form3 frm3 = new Form3();
public Form1()
{
InitializeComponent();
frm2.delPassData += new Form2.delPassDataEventHandler(frm2_delPassData); //declaring a new event handler for Form2
frm3.delPassData += new Form3.delPassDataEventHandler(frm3_delPassData); //declaring a new event handler for Form3
}
void frm3_delPassData(double od, double id, double length, double yield) //this routine communicates with delegate in Form3
{
labelOD.Text = String.Format("{0:F3}", od);
labelID.Text = String.Format("{0:F3}", id);
labelLength.Text = String.Format("{0:F3}", length);
labelYield.Text = String.Format("{0:N0}", yield);
}
void frm2_delPassData(string city, string state) //this routine communicates with delegate in Form2
{
labelCity.Text = city;
labelState.Text = state;
}
private void buttonOpenForm2_Click(object sender, EventArgs e) //launch Form2
{
frm2.Show();
}
private void buttonOpenForm3_Click(object sender, EventArgs e) //launch Form3
{
frm3.Show();
}
}
}
}
}您可以将事件初始化为公共事件delPassDataEventHandler delPassData=委托{}@MukeshRawat这是一个相当苛刻的解决方案。如果没有要引发的事件,为什么要引发它并降低应用程序的速度?@OndrejJanacek调用没有主体的方法是一个非常快的操作。事实上,按照检查空值的顺序。@Servy好的,但我仍然觉得这是一个相当愚蠢的解决方案。鲍勃,你能举一个简单的例子来说明这个问题吗。事实上,您有很多代码实际上与复制问题无关,如果您能够删除复制问题所不需要的代码,那么对它们进行排序就会容易得多。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace SendDataWithDelegate
{
public partial class Form2 : Form
{
public delegate void delPassDataEventHandler(string city, string state); //declare a delegate to communicate with Form1
public event delPassDataEventHandler delPassData; //declare an event name for the delegate's event handler
public Form2()
{
InitializeComponent();
}
private void buttonSendData_Click(object sender, EventArgs e)
{
if (delPassData != null)
delPassData(textCity.Text, textState.Text); //executes if the "delPassData event" occurrs
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace SendDataWithDelegate
{
public partial class Form3 : Form
{
public delegate void delPassDataEventHandler(double od, double id, double length, double yield); //declare a delegate to communicate with Form1
public event delPassDataEventHandler delPassData; //declare an event name for the delegate's event handler
double ODia;
double IDia;
double Len;
double Sy;
public Form3()
{
InitializeComponent();
}
private void buttonSendData_Click(object sender, EventArgs e)
{
ODia = Convert.ToDouble(textOD.Text);
IDia = Convert.ToDouble(textID.Text);
Len = Convert.ToDouble(textLength.Text);
Sy = Convert.ToDouble(textYield.Text);
if (delPassData != null)
delPassData(ODia, IDia, Len, Sy); //executes if the "delPassData event" occurrs
}
}