Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi There,
I have a static bool type variable say IsConnected.
I want to show a specific form when the value of the variable IsConnected is changed using C#.
How can i do it simply.

Thank you so much in advance.
Posted
Updated 16-Jan-14 4:18am
v2

This is a good use for properties over fields...

C#
public static class A
{
    private static bool _isConnected = false;

    public static bool IsConnected
    {
        get { return _isConnected; }
        set
        {
            if (value != _isConnected)
            {
                _isConnected = value;
                OnConnectedChanged();
            }
        }
    }

    public static event EventHandler ConnectedChanged;

    private static void OnConnectedChanged()
    {
        if (ConnectedChanged != null)
            ConnectedChanged(null, EventArgs.Empty);
    }
}


Then your code subscribes to the ConnectedChanged event, which gives notification whenever the property is changed. You could also inherit INotifyPropertyChanged and do the same thing for additional properties using framework elements. [Edit] Actually I think you have to have an instance class to use INotifyPropertyChanged, but the above will work. [/Edit]
 
Share this answer
 
v3
Comments
BillWoodruff 16-Jan-14 14:46pm    
+5 Hi Ron, you cannot use 'this in a static Class. It is also the case with this code that the subscriber's EventHandler will have to access the boolean value through a reference to the static Class since a value type, bool, cannot be passed as a parameter as either sender or standard 'EventArgs.

Perhaps the boolean value could be passed using a custom EventArgs ... although I've never tried, or thought about, using such with in a static context, like this.

It's also the case here that since the value is set to 'false by default, if the first time the user of the Class sets the value of the boolean: it is set to 'false, there will be no notification ... which is probably no big deal.

I'll post a variation on your solution below just for reference sake :)
Ron Beyer 16-Jan-14 14:48pm    
Yes, you are right, you have to replace this with null. I will edit my solution.
WaseemAmin 17-Jan-14 4:26am    
Thank you so much Sir Ron Beyer . I tried your solution and it is really good. I used the Sir BillWoodruff's Solution which is the variation in your solution. It Really worked for me.
Thank You again.
giri001 17-Jan-14 22:42pm    
+5...easy to understand
Here's a slight variation on Ron Beyer's solution, and I encourage you to vote his solution 5.
C#
public static class A
{
    private static bool _isConnected;

    public static bool IsConnected
    {
        get { return _isConnected; }
        set
        {
           // test removed so we get notification every time
           _isConnected = value;
           OnConnectedChanged(_isConnected);
        }
    }

    public static event EventHandler ConnectedChanged;

    // language purists are going to hate what I am
    // doing here ... but it works, and it has worked
    // for many, many, years :)
    private static void OnConnectedChanged(bool boolValue)
    {
        if (ConnectedChanged != null)
            ConnectedChanged(boolValue, EventArgs.Empty);
    }
}
A sample test:
C#
private void Form1_Load(object sender, EventArgs e)
{
    A.ConnectedChanged += A_ConnectedChanged;

    // test
    A.IsConnected = false;
    A.IsConnected = true;
    A.IsConnected = false;
}

private void A_ConnectedChanged(object sender, EventArgs e)
{
    // note there's no problem converting to bool here
    Console.WriteLine(sender.ToString());
}
Note: it's certainly more "standard," and probably better practice, to define a Custom EventArgs Class to pass the boolean value to subscribers to the notification Event. I have never used a Custom EventArgs in a static context before, and I am curious to try it, to see if there are any "gotchas."

Meanwhile, just accessing the static bool in the Class in EventHandlers is easy enough !
 
Share this answer
 
Comments
Ron Beyer 16-Jan-14 15:12pm    
I 5'd, its a bit odd to call "sender" the value but you are right that you can use a custom event args object. Keeping with the standard EventHandler<t> pattern though, there will always be a sender, you just have to set it to null. There aren't really any "gotcha's" other than the sender being null, so you can't really use the same handler for both static and instance events of the same type.
WaseemAmin 17-Jan-14 4:55am    
Thank you so much all for your good reply. It worked nicely.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900