Events

I know how events/delegates works. its when implementing them that seems to be the problem with me.

now... I have:

a Form

several classes

the form knows nothing, it only knows about 1 class, which it instantiates.

this class instantiates other classes. The form, nor do the other classes, need to know about each other.

now, I have successfully created events which when raised from the main form, one of the classes acts upon it - thats all good.

what I want now, is the complete opposite.

I want to make an event where, in one of these classes (not the class the main form creates) I can raise so that the main form picks it up and acts upon it.

Since I am mostly confused at times when events are involved, can someone give me an example on how I would achieve this please

Thanks!




Answer this question

Events

  • justin tighe

    create an event to do an event to raise an event.

    or something like that.... :-P

    This is good stuff.

    Cheers!



  • DmitryMS

    public delegate void Whatever();

    public static event Whatever MyEvent;



  • dron747

    Yes...good point.

    I treat it like any static definition....use when only one "instance" will be needed.

    That said...I remove the wiring each time it fires in the form.....just get rid of the subscription every time.



  • Gagand

    I looked at that... Can you show us how a static delegate instance can invoke a method on a form instance I'd like to do it but don't know how...



  • Roger_Melb

    Let me recap:

    FormA creates ObjectB

    ObjectB creates ObjectC

    ObjectC fires an Event.

    You want FormA to receive that event.

    Unless FormA has some knowledge about ObjectC, this is impossible.

    What you can do, is have ObjectB receive the event, and in response, fire a similar event. Then all you need do is have FormA subscribe to the event from ObjectB.



  • chirag.dave

    sorry but can you give me an example on how YOU would do it

    more specifically how would i subscribe to the event from the class (not main form) which the main form knows nothing about, and vica versa



  • DJ Pandamonium

    thanks, I was wondering how it would be possible.

    That should do it, a better understanding now!



  • sjb31988

    lol...once I figured it out I went event crazy for a while.

    beware tho, static haters are everywhere.



  • Daikoku

    yeh thats pretty much what im trying to achieve and also making sure its all best practice.

    sorry, im just a "best practice" freak

     

    I guess you could, as suggested/hinted, just perhaps raise it going "up" the way (from one class to the other to the other....etc...) but wondering if this is a valid design



  • R.Tutus

    Do it exactly the same way...

    Create the event in the class.

    Subscribe to the event in the form.

    Fire the event anywhere in the class.



  • Muhammad Akhtar Shiekh

    using System;

    using System.Collections.Generic;

    using System.Text;

    namespace WindowsApplication1

    {

    class EventClass

    {

    public delegate void MakeFormDoSomething();

    public static event MakeFormDoSomething FireEvent;

    public static void RaiseEvent()

    {

    FireEvent();

    }

    }

    }

    //FORM:

    private void Form1_Load(object sender, EventArgs e)

    {

    EventClass.FireEvent += new EventClass.MakeFormDoSomething(EventClass_FireEvent);

    EventClass.RaiseEvent();

    }

    void EventClass_FireEvent()

    {

    MessageBox.Show("Form has recieved event");

    }



  • Enes Kabacaoglu

    Thanks a million, it fought me tooth-and-nail to convert this to legal VB.NET syntax but I got it. Me like, me like a lot. This opens up a lot of new possibilities...



  • cunha

    Ignore them, it solves a real problem. It they get upset about "static", they don't get it. Again, thanks! Now, how am I going to not let it slip out of control... I don't see anything wrong with the approach, as long as the class uses RemoveHandler when it gets disposed... Hmm, that puts a bit of pressure on properly dealing with Dispose(). What is your experience



  • StarsFire

    Let's keep it simple: Form1 creates a Class1 object. Class1 in turn creates a Class2 object. Two ways I can think of:

    #1: Class2 object has a reference to the Class1 object, passed to it by the constructor or a method: add a method to Class1 that raises the event, Class2 calls this method.

    #2: Class1 has a reference to the Class2 object, it should because it created it: Class2 raises an event that Class1 catches, then in turn it raises its own event.

    You could also handle case #2 by passing Class2 an initialized delegate instance but that is really no different from case #1.



  • Events