2

I have multiple instances of forms within my windows form application that I instantiate upon loading the main form (including an instance of the main form itself). I would like them to be shared between classes so I can use them from any location. I also need to use the members of each class through these instances.

Please note that I am very confused as to how this works. I don't know how I should be declaring instances of my forms but I only want one instance of each so that I can use the .Hide() method and also use each form's/class's members.

I've tried making the instances as properties but I am not sure how to proceed. I am not sure if that is correct.

    // get and set for form instances
    public Menu menu { get; set; }
    public RandomFacts randomFacts { get; set; }
    public QuizMenu qm { get; set; }
    public AskHowManyQuestions ahmq { get; set; }

    // in the main form load method
    menu = new Menu();
    randomFacts = new RandomFacts();
    qm = new QuizMenu();
    ahmq = new AskHowManyQuestions();

That code is all within the same 'main' form.

I hope that you can help me with being able to access these instances globally and help me solve this problem. Thank you for reading about my issue.

4

3 Answers 3

4

Trivial example:

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{

    public static void Main()
    {   

        var NewClassInstance = Singleton.GetSingleton().NewClassInstance;
        Singleton.GetSingleton().NewClassInstance.Method();
        var OtherClassInstance = Singleton.GetSingleton().OtherClassInstance;
        var Proparty  = OtherClassInstance.Name;
    }
}



public class Singleton
{
    public NewClass NewClassInstance {get; private set;}
    public OtherClass OtherClassInstance {get; private set;}


    private static readonly NewClass _newClass = new NewClass();
    private static readonly OtherClass _otherClass = new OtherClass();
    private static readonly Singleton _singleton = new Singleton();
    private Singleton()
    {
        NewClassInstance = _newClass;
        OtherClassInstance = _otherClass;
        // Prevent outside instantiation
    }

    public static Singleton GetSingleton()
    {
        return _singleton;
    }
}

public class NewClass
{
    public NewClass()
    {
    }

    public void Method()
    {
    }
}


public class OtherClass
{
    public string Name {get; set;}

    public OtherClass()
    {
    }
}

You can access this intance doing

Singleton.GetSingleton();

Let leave you an other exemple https://dotnetfiddle.net/C1PB05

15
  • So do I just do this for each form in my case? Thanks for the explanation.
    – itzgeez
    Feb 9, 2019 at 21:59
  • This way you can instance each class only one time and acess it any time without create a new instance in your cause you can do it for any class you would like to share Feb 9, 2019 at 22:01
  • Thank you. So in my case, do I change each public form constructor to a private one? And just set the properties below the call to InitializeComponent(); , repeating this process for each class? Sorry I am just confused. @AmadeuAntunes
    – itzgeez
    Feb 9, 2019 at 22:32
  • 1
    @AmadeuAntunes Yes, but it also limits you to one instance per form. If one day you want to instantiate two copies of the same form (for whatever reason), things will get ugly.
    – Vilx-
    Feb 9, 2019 at 22:37
  • 1
    In this exemple yes Feb 9, 2019 at 22:48
2

I only want one instance of each

Move your declarations to their own Class and implement the Singleton Pattern. Since you are dealing with Forms, you can instantiate them on demand, and set them back to null when they are closed by subscribing to their FormClosed event.

Example usage:

MyForms.menu.Show();

Code:

class MyForms
{

    private static Menu _menu = null;
    public static Menu menu {
        get {
            if (_menu == null || _menu.IsDisposed)
            {
                _menu = new Menu();
                _menu.FormClosed += (sender, e) => { _menu = null; };
            }
            return _menu;
        }
    }

    private static RandomFacts _randomFacts = null;
    public static Menu randomFacts
    {
        get
        {
            if (_randomFacts == null || _randomFacts.IsDisposed)
            {
                _randomFacts = new RandomFacts();
                _randomFacts.FormClosed += (sender, e) => { _randomFacts = null; };
            }
            return _menu;
        }
    }

    private static QuizMenu _qm = null;
    public static QuizMenu qm
    {
        get
        {
            if (_qm == null || _qm.IsDisposed)
            {
                _qm = new QuizMenu();
                _qm.FormClosed += (sender, e) => { _qm = null; };
            }
            return _qm;
        }
    }

    private static AskHowManyQuestions _ahmq = null;
    public static AskHowManyQuestions ahmq
    {
        get
        {
            if (_ahmq == null || _ahmq.IsDisposed)
            {
                _ahmq = new AskHowManyQuestions();
                _ahmq.FormClosed += (sender, e) => { _ahmq = null; };
            }
            return _ahmq;
        }
    }

}
2

Just assign them to properties on your forms

// in the main form load method

// Class which holds all of the state shared by these forms
sharedState = new SharedState();

menu = new Menu() { SharedState = sharedState };
randomFacts = new RandomFacts() { SharedState = sharedState };
ahmq = new AskHowManyQuestions() { SharedState = sharedState };
qm = new QuizMenu() { SharedState = sharedState };

Obviously, your forms need to declare a SharedState property:

public SharedState SharedState { get; set; }
5
  • One thing of note, if you don't keep a parameterless constructor for the form in addition to the constructor that takes in the SharedState the visual designer for the form may not work. Feb 9, 2019 at 22:24
  • @ScottChamberlain fair point - edited to use properties instead, which will be easier grok
    – canton7
    Feb 9, 2019 at 22:27
  • @ScottChamberlain I was afraid of that too, but oddly enough I've never had any problems with it. I've made forms with a single private constructor and forms with a single parametrized constructor and the designer hasn't complained once. I suspect it doesn't actually instantiate them, it just parses the InitComponents method.
    – Vilx-
    Feb 9, 2019 at 22:48
  • @Vilx- I may have been thinking of custom controls instead of a form. I believe if you are making a custom control and do it when you try to add it in the designer on a form it messes up. Feb 9, 2019 at 22:51
  • @ScottChamberlain - Now that you mention it... maybe it was me who did custom controls and it worked? It was long ago, and I don't really remember anymore... Anyways, I guess the moral is - it might fail, see for yourself if it works in your case.
    – Vilx-
    Feb 10, 2019 at 20:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.