0

I have a static class I was using because I didn't like the idea of passing around a gigantic settings file, but then I wished to be able to have instances subscribe to static events on the static class.

I was looking into using the PropertyChangedEventManager's AddListener method, but it needs an instance to add.

is this even possible? i'm on .net 4.0, in case it matters.

James Joshua Street
  • 3,259
  • 10
  • 42
  • 80
  • 5
    Try it, and see what happens. If it doesn't work, let us know specifically *why* it doesn't work, along with the code that you're using. – Servy Jan 19 '15 at 21:47
  • have you consulted with the `MSDN Documentation`[PropertyChangedEventManager](http://msdn.microsoft.com/en-us/library/system.componentmodel.propertychangedeventmanager%28v=vs.110%29.aspx) – MethodMan Jan 19 '15 at 21:54
  • Simple list of `WeakReference` where T is a delegate? – Chris Jan 19 '15 at 22:12

1 Answers1

0

You can have a static event and have multiple instances subscribe to it. You'll just have to keep in mind all instances that are wired will get notified of event and their implementation invoked. This also can have issues in memory management, your instances won't go out of scope and get GC'd until they unwire themselves from the event.

Heres an example script to show:

delegate void Pop();
static event Pop PopEvent;

void Main()
{
    var t1= new Thing("firstone");
    var t2= new Thing("secondOne");

    //fire event
    PopEvent();
}

public class Thing
{
    public Thing(string name)
    {
        this.name = name;
        PopEvent += this.popHandler;
    }

    private string name="";

    public void popHandler()
    {
        Console.WriteLine("Event handled in {0}",this.name);
    }

}

Output:

Event handled in firstone Event handled in secondOne

Matt
  • 3,638
  • 2
  • 26
  • 33
  • The question is specifically asking about using an event pattern that uses weak references specifically so that it *doesn't* keep the referenced objects alive for the remainder of the applications lifetime. This doesn't address that at all. – Servy Jan 19 '15 at 21:59
  • yes, that appears to be what i'm trying since I can't get the weak event pattern to work. But when I register to a static event and subscribe to it using the static class, visual studio doesn't seem to recognize that the event has been used, which is creating an annoying warning. I can disable it though I guess, though I feel like it shouldn't be warning me at all. Why doesn't it recognize the use of the static event? – James Joshua Street Jan 19 '15 at 22:01
  • @JamesJoshuaStreet The warning is because the backing field is not used. You should raise the event somewhere, then that warning will go away. Do not ignore warnings. – Jeppe Stig Nielsen Jan 19 '15 at 22:04
  • Since event `PopEvent` and method `Main` must reside inside a class (or struct) I guess everything we see above is contained in an "outer" class? The delegate type `Pop` and the class `Thing` can be nested in an "outer" class, or they can be non-nested (declared as direct members of the `namespace`). – Jeppe Stig Nielsen Jan 19 '15 at 22:06
  • They can be non-nested. This was just a copy/paste from LinqPad. The only thing that can fire the event has to be in the same class as where the event is declared. – Matt Jan 19 '15 at 22:35
  • i must be going crazy then. This looks like a subscription to me: SettingsManager.ErrorRaised += new EventHandler(SettingsManager_ErrorRaised); – James Joshua Street Jan 19 '15 at 22:48
  • That looks correct. Can you post the code? Specifically the delegate declaration and the event declaration? – Matt Jan 19 '15 at 22:50
  • Ah it's ok. I think i realized my mistake. i had forgotten all the steps in creating an event. I think I need a handler defined on the object with the event. Whoops – James Joshua Street Jan 19 '15 at 22:54