我已经创建了一个小型应用程序,但现在我想合并一些可以通过列表框查看的日志记录。数据源可以从任意数量的地方发送。我创建了一个新的日志类,它将传入一个委托。我认为我接近解决方案,但我收到 NullReferenceException 并且我不知道正确的解决方案。这是我尝试做的一个例子:
Class1 where the inbound streaming data is received.
class myClass
{
OtherClass otherClass = new OtherClass();
otherClass.SendSomeText(myString);
}
日志记录类
class OtherClass
{
public delegate void TextToBox(string s);
TextToBox textToBox;
Public OtherClass()
{
}
public OtherClass(TextToBox ttb)
{
textToBox = ttb;
}
public void SendSomeText(string foo)
{
textToBox(foo);
}
}
表格
public partial class MainForm : Form
{
OtherClass otherClass;
public MainForm()
{
InitializeComponent();
otherClass = new OtherClass(this.TextToBox);
}
public void TextToBox(string pString)
{
listBox1.Items.Add(pString);
}
}
每当我在 myClass 中收到数据时,它都会引发错误。您可以提供的任何帮助将不胜感激。