Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hello everyone.

 

Here's a scenario involving events that I've already tested but am curious about the details. In a Windows Forms application, the main form (of type, Class1) instantiates a new object of type, Class2. This new object instantiates an object of type, Class3.

 

Class1

|

Class2

|

Class3

 

An event is fired in the Class1 object. I placed event handlers in the Class2 and Class3 objects to deal with this event, but when the event fires, only the event handler in the Class2 object is executed.

 

Is it true that only objects that were instantiated by an object of a particular class are able to subscribe to that class' events?

 

If so, what is the best way to let the Class3 object subscribe to the events in the Class1 object?

 

Thanks for your insight!

  • Leaders
Posted

Is it true that only objects that were instantiated by an object of a particular class are able to subscribe to that class' events?

 

Nope! Maybe you should show your code that declares, raises, and subscribes to events. Any object can subscribe to events on any other object it can get its hands on.

[sIGPIC]e[/sIGPIC]
Posted

Snarfblam,

 

Thanks for the reply. I had already blown away the code that I was talking about in my first post, but I went back and created some more. This time, I could get NO response to the event.

 

I have Form1, which is the main form. It has two buttons -- the first button creates an instance of Class2, and the second button fires the custom event (MyEvent). When the user clicks on the first button, an instance of Class2 is created. In the Class2 constructor, an instance of Class3 is created.

 

Here is the code for the main form:

 

namespace EventsTesting
{
   public partial class Form1 : Form
   {
       //Declare a public event
       public event EventHandler MyEvent;

       //Declare a variable for a Class 2 object
       Class2 secondClass;

       public Form1()
       {
           InitializeComponent();
       }

       //Create Class2 button click event handler
       private void btnCreateClass2_Click(object sender, EventArgs e)
       {
           //Instantiate a new Class2 object
           secondClass = new Class2();
       }

       //Fire event button click event handler
       private void btnFireEvent_Click(object sender, EventArgs e)
       {
           //If there are any subscribers to the event
           if (this.MyEvent != null)
           {
               //Fire the event
               this.MyEvent(this, new EventArgs());
           }
       }
   }
}

 

Here is the code for Class2:

 

namespace EventsTesting
{
   class Class2
   {
       //Declare a Form1 object
       Form1 parentClass;

       //Declare a Class3 object
       Class3 childClass;

       public Class2()
       {
           //Instantiate the Form1 object
           parentClass = new Form1();

           //Add an event handler for the Form1 event
           parentClass.MyEvent += new EventHandler(parentClass_MyEvent);

           //Notify the user that Class2 has been instantiated
           MessageBox.Show("Class 2 created!");

           //Instantiate the Class3 object
           childClass = new Class3();
       }

       //Parent class' MyEvent event handler
       void parentClass_MyEvent(object sender, EventArgs e)
       {
           //Show a user message
           MessageBox.Show("Event consumed in Class 2");
       }
   }
}

 

And finally, the code for Class3:

 

namespace EventsTesting
{
   class Class3
   {
       //Declare a Form1 object
       Form1 grandParentClass;

       public Class3()
       {
           //Instantiate the Form1 object
           grandParentClass = new Form1();

           //Add an event handler for the Form1 MyEvent event
           grandParentClass.MyEvent += new EventHandler(grandParentClass_MyEvent);

           //Notify the user that the Class3 object is instantiated
           MessageBox.Show("Class 3 created!");
       }

       //Event handler for Form1 MyEvent
       void grandParentClass_MyEvent(object sender, EventArgs e)
       {
           //Show user message
           MessageBox.Show("Event consumed in Class 3");
       }
   }
}

 

When I fired the event, nothing happened. When I debugged the program, I found that there were no subscribers to the event.

 

I'm sure this is a very elementary problem, but any help in understanding would be greatly appreciated. Thanks for your time!

  • Administrators
Posted

In the Class2 constructor you are doing

  parentClass = new Form1();

this is creating a new instance of the Form1 class and then attaching your event handlers to that instance. This is a bit like setting your alarm clock, going to the shops and buying a new alarm clock and expecting it to wake you up at the correct time.

 

If you want to handle events from the original Form1 instance you would need to pass this instance to Class2 rather than create a new instance.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...