locked
Call Instance Method From Static Method RRS feed

  • Question

  • 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.

    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

    Form1._instance.Show();

    would check for an existing instance, and if one is not present, the class would be instantiated.

    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?

     

    *I would have placed the pieces of code in code blocks, but I am prompted with a message stating that some features are currently disabled on the site*

    • Edited by mcfly1204 Tuesday, December 6, 2011 7:29 PM
    Tuesday, December 6, 2011 7:27 PM

Answers

  • Since you have an instance, you can use it to call your instance method:

    public static void SetAccountID(string account)
    {
                Form1.Instance.Show(); // This should use the property, not the backing field...
                Form1.Instance.LoadAccounts(); // use the instance here, too!
    
    }
    



    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Proposed as answer by servy42 Friday, December 9, 2011 7:06 PM
    • Marked as answer by Reed Copsey, JrMVP Thursday, December 15, 2011 5:52 PM
    Tuesday, December 6, 2011 8:09 PM
  • Should it be something like this?

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

    I'm not sure since I don't know the rest of the code. In any case if you want to call an instance method then you must have an instance. Why would you want a static method to call an instance method? It's not something I would expect to happen. I try to limit static methods to classes that provide utility methods that have no side effects. For a singleton, I'd probably make the constructors private and provide a method that creates the sole instance if it does not exist and just returns it if it does. It's not obvious that your code does that.


    Regards David R
    ---------------------------------------------------------------
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.
    • Proposed as answer by servy42 Friday, December 9, 2011 7:07 PM
    • Marked as answer by Reed Copsey, JrMVP Thursday, December 15, 2011 5:52 PM
    Tuesday, December 6, 2011 8:19 PM

All replies

  • Since you have an instance, you can use it to call your instance method:

    public static void SetAccountID(string account)
    {
                Form1.Instance.Show(); // This should use the property, not the backing field...
                Form1.Instance.LoadAccounts(); // use the instance here, too!
    
    }
    



    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Proposed as answer by servy42 Friday, December 9, 2011 7:06 PM
    • Marked as answer by Reed Copsey, JrMVP Thursday, December 15, 2011 5:52 PM
    Tuesday, December 6, 2011 8:09 PM
  • Should it be something like this?

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

    I'm not sure since I don't know the rest of the code. In any case if you want to call an instance method then you must have an instance. Why would you want a static method to call an instance method? It's not something I would expect to happen. I try to limit static methods to classes that provide utility methods that have no side effects. For a singleton, I'd probably make the constructors private and provide a method that creates the sole instance if it does not exist and just returns it if it does. It's not obvious that your code does that.


    Regards David R
    ---------------------------------------------------------------
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.
    • Proposed as answer by servy42 Friday, December 9, 2011 7:07 PM
    • Marked as answer by Reed Copsey, JrMVP Thursday, December 15, 2011 5:52 PM
    Tuesday, December 6, 2011 8:19 PM
  • With that code, I receive the error "an object reference is required for the non-static field method or property".

     

    *edit*

    After changing Instance to being static, I can now call Form1.Instance from within the same class and also from my other class with the following:

     

    Form1.NewAcctCreated(accountID);


    • Edited by mcfly1204 Tuesday, December 6, 2011 9:25 PM
    Tuesday, December 6, 2011 8:46 PM
  • Hi mcfly1204,

    I'm glad to hear that you got it working. If you have any difficulty in future programming, we welcome you to post here again.


    Bob Shen [MSFT]
    MSDN Community Support | Feedback to us
    Thursday, December 8, 2011 7:11 AM