Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey,

UPDATE: Solution 1 accepted!

I've been programming in C# for a few weeks (nearly fulltime) and learning every day. I am currently trying to organize the data in my project. This question is a followup question from my previous question Passing data between forms. I have for example some settings which I want to adjust. These settings are used in the main form but set in a different form.

I'd like to have a central point in my project where I can store all my settings (variables). It is important that I can acces and update the data from all froms in my project.

I create for example a class with all the declarations with a default value. like this:

C#
namespace SerialProgrammer
{
    class Settings
    {
        public decimal Density = 21;
        public string SelectedPrinterName = "ZDesigner GX430t";
    }
}



So I want to adjust this value in form2; like this:

C#
Settings settings = new Settings();
private void BTN_Set_Click(object sender, EventArgs e)
{

    settings.Density = nudDensity.Value;
    this.Close();

}


And on my main form I want to use the settings, like this:

C#
Settings settings = new Settings();
private void printLabel(string snum, string type, int number)
{
    if (IsPrinterOnline(settings.SelectedPrinterName))
    {
        if (settings.SelectedPrinterName.Contains(Dymo_PrinterName))
        {
            dymoPrintLabel(snum, type, number);
        }
        else if (settings.SelectedPrinterName.Contains(Zebra_PrinterName))
        {
            ZebraPrintLabel(snum, type, number);
        }

    }
}


What I experienced is that even though it looks like (at least for me) that both forms share the class Settings, they both use a different class Settings with different values. How can I change my code that I have one set of settings which are easily accessable from both forms?
Posted
Updated 2-Oct-12 5:03am
v2

Another possibility for you would be to use the singleton pattern by having a static object.
Please see this code for your class:
C#
class Settings
{
   private Settings()
   {
   }
   static Settings m_instance = null;
   public static Settings Core
   {
      get
      {
            if (m_instance == null)
               m_instance = new Settings();
            return m_instance;
      }
   }
   public decimal Density = 21;
   public string SelectedPrinterName = "ZDesigner GX430t";
}

You can then use the class from wherever you want using the follwing code:
C#
public Form1()
{
   InitializeComponent();
   Settings.Core.Density = 5;
}
 
Share this answer
 
Comments
pieterjann 2-Oct-12 10:32am    
OK, and if I want to change the SelectedPrinterName I'd use the following line: Settings.Core.SelectedPrinterName = "Some printername"; and reading the settings would be like: decimal temp = Settings.Core.Denisty; And will this work from each form in my project (as long as in the same namespace)
JF2015 2-Oct-12 10:33am    
Yes it will. There is only this one "global" object and not multiple instances of it.
pieterjann 2-Oct-12 10:35am    
Thanks, I'm gonna make a test project where I experiment with this! I've got about 1 hour left today, if this works today you are my hero! (and if it does tomorrow, you still are =D)
pieterjann 2-Oct-12 10:55am    
Got it! THANKS!
JF2015 2-Oct-12 10:57am    
Great!!! Finally!
You are sharing the same class (Type) but not the same instance of that class (Object).
You should explore the usage of the keyword static if you want to have such "global" object or even better have a llok at the singleton pattern. You'll find plenty of articles in code project about it.
 
Share this answer
 
This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

Please also see other solutions in this discussion. If the application is simple enough, the solution could be as simple as declaring of some internal property in one form and passing a reference to the instance of one form to the instance of another form. For more complex projects, such violation of strictly encapsulated style and loose coupling could add up the the accidental complexity of the code and invite mistakes, so the well-encapsulated solution would be preferable.

Please see also:
http://en.wikipedia.org/wiki/Accidental_complexity[^],
http://en.wikipedia.org/wiki/Loose_coupling[^].

—SA
 
Share this answer
 

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