smriti
smriti

Reputation: 445

problem in delegate program code

I made this code in a class library named usedelegates:

namespace useofdelegates
{
    public class Class1
    {   //int i;
        public delegate void mydelegate(object sender,EventArgs e);
        public event mydelegate myevent;
        public void fire()
        {
           EventArgs ee = new EventArgs();
            myevent(this,ee);
        }

    }
}

Then in a windows form application, I intended to fire this event on the click of a button. The code in the form application is:

namespace WindowsFormsApplication9
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        useofdelegates.Class1 ob;
        private void button1_Click(object sender, EventArgs e)
        {
           // ob = new useofdelegates.Class1();
            ***ob.fire();***//give exception as object reference not set to an instance of     an object.*/
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            useofdelegates.Class1 ob = new useofdelegates.Class1();
            ob.myevent+=new useofdelegates.Class1.mydelegate(ob_myevent);
           // ob.fire();
        }
        public void ob_myevent(object sender, EventArgs e)
        {
            MessageBox.Show("hello hapiness");
        }


    }
}

This code on compiling throws an exception:

Object reference not set to an instance of an object.

but when I call ob.fire() in form_load(), it gives me the desired result without any exception. Why is this happening?

Upvotes: 1

Views: 157

Answers (2)

Oded
Oded

Reputation: 499002

Several things:

The ob object is not a class variable (field) so each function needs to initialize it, register the event and then call it. You are doing this on the form load, but not on the button click.

Or rather, looking at your code, you are hiding the field in your form load by using a method variable with the same name.

This should work fine:

useofdelegates.Class1 ob;
private void button1_Click(object sender, EventArgs e)
{
  ob.fire();
}

private void Form1_Load(object sender, EventArgs e)
{
  ob = new useofdelegates.Class1();
  ob.myevent+=new useofdelegates.Class1.mydelegate(ob_myevent);
}

You also need to check that the delegate object is not null before calling it in the class that defines the event and delegate:

if (myevent != null)
   myevent(this,ee);

The thread safe version is this:

mydelegate eventcopy = myevent;
if (eventcopy != null)
   eventcopy(this,ee);

Upvotes: 3

Paul Keister
Paul Keister

Reputation: 13077

You are hiding the member declaration for ob by redeclaring it as a local variable in Form_Load. Here's the way it should look:

    private void Form1_Load(object sender, EventArgs e)
    {
        ob = new useofdelegates.Class1();
        ob.myevent+=new useofdelegates.Class1.mydelegate(ob_myevent);
       // ob.fire();
    }

You are probably getting a waring about this from your compiler. It's always a good idea to try to eliminate compiler warnings.

Upvotes: 1

Related Questions