0
Follow
2
View

Call async inside public static bool

cuiling2010 注册会员
2023-01-26 11:55

Well, static means not instance, so you have one and only one Called value for all Form1 instances. When you try to call Update() within static property set, the compiler complains: it doesn't know on which instance should it be called (imagine, that there are three opened Form1).

You can either add some logic, e.g. let call Update on all opened Form1:

using System.Linq;

...

private static bool Called = false;

public static bool HideButton
{
    get { return Called; }
    set
    {
        if (Called != value)
        {
            Called = value;

            // Assuming WinForms  
            // We call Update on all opened Form1 forms
            foreach (var form in Application.OpenForms.OfType())
                form.Update(); 
        }
    }
}

Or you may let each Form1 instance have its own Called, i.e. drop static:

private bool Called = false;

public bool HideButton
{
    get { return Called; }
    set
    {
        if (Called != value)
        {
            Called = value;

            Update(); 
        }
    }
}

Please, note, that async is quite a different conseption which is orthogonal to static

About the Author

Question Info

Publish Time
2023-01-26 11:55
Update Time
2023-01-26 11:55