views:

104

answers:

4

I have created a small application but I would now like to incorporate some type of logging that can be viewed via listbox. The source of the data can be sent from any number of places. I have created a new logging class that will pass in a delegate. I think Im close to a solution but Im receiving a NullReferenceException and I don’t know the proper solution. Here is an example of what Im trying to do:

Class1 where the inbound streaming data is received.
class myClass
{
   OtherClass otherClass = new OtherClass();
   otherClass.SendSomeText(myString);
}

Logging Class

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);
   }
}

The Form

public partial class MainForm : Form
   {
   OtherClass otherClass;

   public MainForm()
   {
       InitializeComponent();
       otherClass = new OtherClass(this.TextToBox);
   }

   public void TextToBox(string pString)
   {
       listBox1.Items.Add(pString);
   }

}

Whenever I receive data in myClass, its throwing an error. Any help you could give would be appreciated.

+1  A: 

Remove the empty constructor and pass the proper delegate in.

class OtherClass
{
   public delegate void TextToBox(string s);

   private readonly TextToBox textToBox;

   public OtherClass(TextToBox textToBox) 
   {
       if (textToBox == null)
           throw new ArgumentNullException("textToBox");

       this.textToBox = textToBox;
   }

   public void SendSomeText(string foo)
   {
       textToBox(foo);
   }
}
ChaosPandion
Sorry if Im a little new at this but if I removed the empty constructor how would I pass in the proper delegate from MyClass? It doesnt have a reference to TextToBox like the Form does.
Leroy Jenkins
@Leroy - Where is `MyClass` initialized?
ChaosPandion
@Chaos I was hoping to simplify the problem with this example but it looks like I would have been better off just putting the whole code. Form1 calls an API (Class1) and that class is where MyClass is being initialized. MyClass receives streaming data that I would like to log in the Form.
Leroy Jenkins
+1  A: 

Change your OtherClass to check for null:

class OtherClass
{
   public delegate void TextToBox(string s);

   TextToBox textToBox;

    Public OtherClass()
    {
    }

   public OtherClass(TextToBox ttb) 
   {
       textToBox = ttb;
   }

   public void SendSomeText(string foo)
   {
       var handler = this.TextToBox;
       if(handler != null)
       {
           textToBox(foo);
       }
   }
}

Now the reason you're getting the exception though is because in your myClass when you're creating a new OtherClass, you're not providing a method the delegate should "point" to. Therefore, when you're OtherClass calls textToBox(foo); there's no method behind it, and it blows up.

BFree
I feel that if `textToBox == null` the class is in an invalid state.
ChaosPandion
+1  A: 

You should pass in myClass constructor you OtherClass instance created in MainForm, don't create OtherClass instance in myClass it's not the instance to which you attached handler.

Andrew
+1  A: 

In myClass, you're not calling the overloaded OtherClass constructor that takes a TextToBox, so textToBox(foo) fails because textToBox has not been set.

Can you show the code where myClass is initialized and called?

DShultz