Call Instance Method From Static Method (C#)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mcfly1204
    New Member
    • Jul 2007
    • 233

    Call Instance Method From Static Method (C#)

    I have two classes in the same namespace, Form1 and NewAccount. I am attempting to call an instance method that will reload a combobox on Form1. I have Form1 implemented as a singleton, and a static method in Form1, but I cannot access the instance method 'LoadAccounts() ' which will reload the combobox.

    Code:
    public static void SetAccountID(string account)
            {
                Form1._instance.Show(); //checks for an open instance
                Form1.LoadAccounts(); //not sure how to/if I can call instance function          
            }

    My understanding of the singleton pattern was that the call

    Code:
    Form1._instance.Show();
    would check for an existing instance, and if one is not present, the class would be instantiated.
    Code:
    public Form1 Instance
            {
                get
                {
                    if (Form1._instance == null)
                    {
                        Form1._instance = new Form1();
                    }
    
                    return Form1._instance;
                }
            }
    
    private static Form1 _instance = null;
    If this means that Form1 will always exist when this is called, I thought I would then be able to call any instance method of that class, which does not seem to be the case. Can I simply not call instance methods from a static method?
  • mcfly1204
    New Member
    • Jul 2007
    • 233

    #2
    After firing this line of code in the static method

    Code:
    Form1._instance.Show();
    how do I access this instance, i.e., how do I call instance method for this instance? Am I even on the correct path, or is my line of thinking off on this?

    Comment

    Working...