我已經創建了一個小應用程序,但我現在想要合併某些類型的可通過列表框查看的日誌記錄。數據的來源可以從任意數量的地方發送。我創建了一個新的日誌類,它將傳入一個委託。我認爲我接近一個解決方案,但我收到一個NullReferenceException,我不知道正確的解決方案。這裏是什麼林試圖做一個例子:使用委託記錄類(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的接收數據,它拋出一個錯誤。任何幫助你可以給予讚賞。
請添加異常文本和調用堆棧 – 2010-04-14 17:45:20