I have three classes, one class loads in a configuration file into memory for access later on. My other class is the mainform. What I am attempting to achieve is that when certain elements of the configuration class are loaded in they are added to a listview in the GUI (WindowsForm).
I know you can’t access the GUI directly from the other non-mainform classes, which after reading I don’t want to do anyway, so I have been attempting to fire events to say “config updated” that the mainform will listen for and update the listview when appropriate. So I created a third class that defines the events and the delegates etc but in all the examples I’ve seen if different classes are calling the event they are all passed a shared instance of the event class.
Is this the way I should do things? When I created the configuration class from the mainform class should I pass an instance of the event class? Or is there a way for 2 classes that don’t know anything about each other to share an event?
I’ve modified a microsoft example below to semi demonstrate what I’m needing.
using System;
public class FireEventArgs : EventArgs
{
public FireEventArgs(string room, int ferocity)
{
this.room = room;
this.ferocity = ferocity;
}
public string room;
public int ferocity;
}
public class FireAlarm
{
public delegate void FireEventHandler(object sender, FireEventArgs fe);
public event FireEventHandler FireEvent;
public void ActivateFireAlarm(string room, int ferocity)
{
FireEventArgs fireArgs = new FireEventArgs(room, ferocity);
if(FireEvent!=null)FireEvent(this, fireArgs);
}
}
public class FireEventTest
{
public static void ExtinguishFire(object sender, FireEventArgs fe)
{
Console.WriteLine("\nThe ExtinguishFire function was called by {0}.", sender.ToString());
if (fe.ferocity < 2)
Console.WriteLine("This fire in the {0} is no problem. I'm going to pour some water on it.", fe.room);
else if (fe.ferocity < 5)
Console.WriteLine("I'm using FireExtinguisher to put out the fire in the {0}.", fe.room);
else
Console.WriteLine("The fire in the {0} is out of control. I'm calling the fire department!", fe.room);
}
public static void Main()
{
FireAlarm myFireAlarm = new FireAlarm();
FireAlarm fireAlarm = new FireAlarm();
fireAlarm.FireEvent += new FireAlarm.FireEventHandler(ExtinguishFire);
myFireAlarm.ActivateFireAlarm("Kitchen", 3);
myFireAlarm.ActivateFireAlarm("Study", 1);
myFireAlarm.ActivateFireAlarm("Porch", 5);
return;
}
}
Thanks in advance for any help.
Well, I don’t think that I understand you fully, but you can have a class called EventDispatcher, make it Singleton if you want, and inside it have a public event
ConfigurationElementLoaded(object sender, ConfigurationElementLoadedArgs args)and a public method to fire the event:
in which you fire the event.
Your mainForm can subscribe via the Singleton isntance :
EventDispatcher.Instance.ConfigurationElementLoaded += ...and your Configuration can fire the event with
FireConfigurationElementLoaded.If this answer doesn’t help, please elaborate more what exactly you want to do…