Introduction
I just had a conversation with one of my colleagues and he mentioned the subject of using Static Events which was new to me and I want to investigate it in this article.
Background (Why Static?)
Basically the idea is to have something shared among all the loaded instances of a class and ensure that changing the static property will cause all instances to update their content right away without the changer having to iterate through the existing objects and figure out which ones need to be updated. Kind of building the intelligence into the class so that all instances know what to do when the static property has changed.
This reminds me of the example of exchange rate, which is very well known to those implementing in banking systems: all transactions respect the current exchange rate. But I don’t recall using Static Events for that. We saw this property as a separate object and we made sure that there is only one instance of it at a time. And all instances of transactions knew where to find it when needed. There is a fine difference though. The transactions will not need to know about the changes happening on the exchange rate, rather they will use the last changed value at the time that they use it by requesting the current value. This is not enough when, for example, we want to implement an application where the user interface reacts immediately on changes in the UI characteristics like font, as if it has to happen at real-time. It would be very easy if we could have a static property in the Font
class called currentFont
and a static method to change that value and a static event to all instances to let them know when they need to update their appearance.
Using the code
It is clear that we need a static field that all instances respect. Let’s say we need a static field called Font
that all the labels will use to refresh when this base field has been changed.
public class MyLabel : System.Windows.Forms.Label
{
// The static field all class instance will respect
private static Font font = new Font("Verdana",11 );
This field requires a static property setter to allow us to make changes to the base field.
public static Font Font
{
set {
font = value;
OnMyFontChange(new FontChangedEventArgs(font));
}
}
As you can see this is where we set the static variable but we also call the notification method to start the delegate.
private static void OnMyFontChange(FontChangedEventArgs e) { if (MyFontChanged != null) MyFontChanged(null, e); }
Now almost everything is set. All we need to do is to make sure that every instance subscribes to this event. And that is what we do in the constructor of the class.
public MyLabel()
{
// Every instance subscribes to this event
MyLabel.MyFontChanged += new FontChangedEventHandler(this.ChangeBaseFont);
}
The delegated method is where we use the changed value to refresh the UI.
private void ChangeBaseFont(object sender, FontChangedEventArgs e)
{
base.Font = e.Font ;
base.Invalidate();
}
Note:
Of course we could access the static field without the need to pass it over and introduce a new EventArgs
class but it just happens to be implemented so, and it certainly has nothing to do with this subject.
In the demo, I have provided a test application using this label control and it demonstrates how it will update multiple screens by changing the base Font
property.