0

I have my class where I define my event:

public class EventRaiserUtility
{
    public event EventHandler updateList;

    public void updateListEvent()
    {
        if (updateList != null)
        {
            updateList(this, EventArgs.Empty);
        }
    }
    public static EventRaiserUtility raiser = new EventRaiserUtility();
}

and this is where I raise my event:

EventRaiserUtility.raiser.updateListEvent();

and finally this is where I'm trying to create the listener:

...
EventRaiserUtility.raiser.updateList += new EventHandler(raiser_updateList);
//placed in the init method of another class
...

private void raiser_updateList(object sender, EventArgs e)
{
    connType = MainWindowViewModel.getTTC();
}

Simply: this event has to notify when a list is updated and then update another list, with getTTC() with raiser_updateList.

But raiser_updateList is never called. Why? All my 3 snippets of code are in 3 different classes (same project), but this isn't a problem... right?

Piero Alberto
  • 3,823
  • 6
  • 56
  • 108
  • You shouldn't edit your question to include the proposed solutions, so that folks can see the initial issue and solutions. – Novitchi S Oct 10 '14 at 10:00

3 Answers3

5

You're creating a new EventRaiserUtility just before you call updateListEvent (which should have a capital U to follow .NET conventions, by the way; ditto updateList => UpdateList) - but you're creating a separate EventRaiserUtility in order to subscribe to the event. They're different objects, so have different event subscribers. If you always create a new object before raising the event, there could never be any subscribers.

You should have a single EventRaiserUtility stored in an instance variable in the containing class - you'd create that on construction, then subscribe to the event in one place an raise it in another... but because they'd be talking about the same EventRaiserUtility object, you wouldn't lose the subscription.

(It's not clear that this utility class actually has much value, to be honest - why aren't you just declaring the event in your class directly? And why declare your own delegate when EventHandler has exactly the same signature?)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I have edited the code with your tips about my delegate. But how can I refer to same instance of the event, if I'm in different classes? How can one see the instance from the other? (sorry for the dumb question, I'm new to the event managing) – Piero Alberto Oct 10 '14 at 09:32
  • 1
    @PieroAlberto: The same way you'd persist any other information. Basically we don't know nearly enough about what you're trying to do, to be honest - but you need one object to really own the event. Maybe that *is* the `EventRaiserUtility`, but you'd still need to make the same instance available in both classes. Without knowing the relationship between those classes, it's hard to help :( – Jon Skeet Oct 10 '14 at 09:37
  • I have done it, thanks to your advices. Look, I've edited again the code, now it's working! :) – Piero Alberto Oct 10 '14 at 09:40
  • @PieroAlberto: That's a *really bad solution* though - that means you'll only ever be able to have a single event raiser in your AppDomain, even if you've got multiple independent objects which want to handle their events independently. Your solution may well be enough for you at the moment, but you should really think about your overall design, and how to make "the thing that wants to subscribe to the event" logically away of "the thing that wants to raise the event" so it can subscribe in a cleaner way. – Jon Skeet Oct 10 '14 at 09:45
  • Ok... do you have some links about this? :) anyway, for now I set your answer like the right one! =) – Piero Alberto Oct 10 '14 at 09:46
  • @PieroAlberto: Not really - it's general object oriented design. For example, if you wanted one class to react to a button click event in a particular form, you'd need that code to have a reference to the right form for it to subscribe to the event. Again, we don't really have enough context to give you more specific advice... – Jon Skeet Oct 10 '14 at 09:48
3

As far as I can see - you are subscribing to the event of one instance of EventRaiserUtility, but raising event from another instance which has no subscribers

Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71
  • how can I refer to same instance of the event, if I'm in different classes? How can one see the instance from the other? (sorry for the dumb question, I'm new to the event managing) – Piero Alberto Oct 10 '14 at 09:32
  • @PieroAlberto Briefly - you can move your `updateList` event to the 'first' class where you raising it, and the 'second' class which is handling event should subscribe to that event from `first` class instance. In this case you may find your `EventRaiserUtility` is redundant. – Andrey Korneyev Oct 10 '14 at 09:40
0

you need one object to really own the event. Maybe that is the EventRaiserUtility, but you'd still need to make the same instance available in both classes. Without knowing the relationship between those classes