Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
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:
 
namespace SerialProgrammer
{
    class Settings
    {
        public decimal Density = 21;
        public string SelectedPrinterName = "ZDesigner GX430t";
    }
}
 

So I want to adjust this value in form2; like this:
 
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:
 
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 2-Oct-12 5:20am
Edited 2-Oct-12 6:03am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Another possibility for you would be to use the singleton pattern by having a static object.
Please see this code for your class:
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:
public Form1()
{
   InitializeComponent();
   Settings.Core.Density = 5;
}
  Permalink  
Comments
pieterjann at 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 at 2-Oct-12 10:33am
   
Yes it will. There is only this one "global" object and not multiple instances of it.
pieterjann at 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 at 2-Oct-12 10:55am
   
Got it! THANKS!
JF2015 at 2-Oct-12 10:57am
   
Great!!! Finally!
gladiatron at 2-Oct-12 11:11am
   
This is effectively what I said in your previous question :). Having the data class separate from the forms and have it be shared between the forms i.e. static. But then I guess you were looking for the exact code to just plug into your solution.
pieterjann at 5-Oct-12 5:57am
   
Yea, I still need some exact code examples sometimes before I can use it. I'm trying very hard to get used to all terms used in C# :)(still a beginner!) thank you for your help!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 495
1 Sergey Alexandrovich Kryukov 419
2 OriginalGriff 360
3 CHill60 310
4 CPallini 275
0 Sergey Alexandrovich Kryukov 10,017
1 OriginalGriff 9,495
2 Peter Leow 5,241
3 Kornfeld Eliyahu Peter 3,373
4 Maciej Los 3,076


Advertise | Privacy | Mobile
Web04 | 2.8.150327.1 | Last Updated 2 Oct 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100