Click here to Skip to main content
15,868,141 members
Articles / Programming Languages / C#
Article

Static Events

Rate me:
Please Sign up or sign in to vote.
2.48/5 (10 votes)
1 Nov 20053 min read 63.6K   458   9   7
Refresh all loaded controls by changing the base fields.

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.

C#
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.

C#
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.

C#
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.

C#
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.

C#
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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Systems Engineer
Netherlands Netherlands
I am a Microsoft certified application developer and work on Microsoft platform since 1993. I like to learn how Microsoft Systems have been built up and dig myself into different libraries in Win32 and .NET. I have worked with C++, VB6, Java, C#, ASP, ASP.Net and PHP.

Comments and Discussions

 
GeneralSingleton Pin
Guido_d1-Nov-05 22:14
Guido_d1-Nov-05 22:14 
GeneralRe: Singleton Pin
Pascal Ganaye1-Nov-05 22:28
Pascal Ganaye1-Nov-05 22:28 
GeneralRe: Singleton Pin
Guido_d1-Nov-05 23:10
Guido_d1-Nov-05 23:10 
GeneralRe: Singleton Pin
Pascal Ganaye2-Nov-05 1:18
Pascal Ganaye2-Nov-05 1:18 
GeneralRe: Singleton Pin
AsgharPanahy2-Nov-05 23:27
AsgharPanahy2-Nov-05 23:27 
Yes. This would be another approach. And again it would remind me to Exchange Rate but a more suffisticated version by enabling the Subscriber and Publisher pattern and still support a signleton.
The difficaulty of that approach is that all the users of that singleton object has to be aware of it and they need to implement a code subscribe and unsubscribe. The Static Event principal on the other hand has been implemented once in the constructor, where the static event is placed and they are nicely glued together. It does subscribe and subscribe aswell but there is not contract outside of the class. Not necessarily much better but certainly easier to maintain.

The one and the only one is no one.
You and I should become one.
Questionstatic events == bad idea? Pin
wickerman.261-Nov-05 9:14
wickerman.261-Nov-05 9:14 
AnswerRe: static events == bad idea? Pin
AsgharPanahy3-Nov-05 0:32
AsgharPanahy3-Nov-05 0:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.