C# 另一个类触发事件后,事件操作返回null
故事是这样的,我有三个表格和两个班级,我现在有信息发送到表格一。现在,当我尝试从同一个类(或另一个类)向form2发送数据时,C# 另一个类触发事件后,事件操作返回null,c#,C#,故事是这样的,我有三个表格和两个班级,我现在有信息发送到表格一。现在,当我尝试从同一个类(或另一个类)向form2发送数据时,public event Action updateMainLabelconn返回null 以下是我正在尝试的策略,也是与form1交谈时有效的策略: public class Connection { public Form2 form2; public event Action<string> updateMainLabelconn;
public event Action updateMainLabelconn代码>返回null
以下是我正在尝试的策略,也是与form1交谈时有效的策略:
public class Connection
{
public Form2 form2;
public event Action<string> updateMainLabelconn;
//There is other methods that call to this method and it works correctly.
public void LE1_Update(object sender, InstrumentLookupSubscriptionEventArgs e)
{
updateMainLabelconn("INSTRUMENT LOADED : " + e.Instrument.InstrumentDetails.Name.ToString());
}
}
public partial class Form2 : Form
{
public Connection conn;
public Form2()
{
InitializeComponent();
conn = new Connection();
conn.updateMainLabelconn += MainStatusUpdateconn;
}
public void MainStatusUpdateconn(string e)
{
mainStatus.Text = e;
}
公共类连接
{
公共表格2表格2;
Belconn公共事件行动更新;
//还有其他方法调用此方法,并且它工作正常。
public void LE1_更新(对象发送方、工具lookupsubscriptioneventargs e)
{
updateMainLabelconn(“加载的工具:+e.INSTRUMENT.InstrumentDetails.Name.ToString());
}
}
公共部分类表单2:表单
{
公共连接;
公共表格2()
{
初始化组件();
conn=新连接();
conn.updateMainLabelconn+=MainStatusUpdateconn;
}
public void mainstatus updatecon(字符串e)
{
mainStatus.Text=e;
}
这就是代码的关键部分,我想,如果我错过了什么,请随意给我一个新的!哈哈有任何错误被抛出吗?是的,UpdateMailableConn返回一个'System.NullReferenceException',因为动作是空的。嗯,我没有时间在我这边运行这个,但在我看来,你的LE1\U更新没有创建ac操作,可能类似于操作(UpdateMailabelconn(“INSTRUMENT LOADED:+e.INSTRUMENT.InstrumentDetails.Name.ToString());?Psudo代码,但我认为这是问题所在……那么,如何调用LE1\u Update
呢?是否通过Form2
实例?