Hi guys I'm wondering if this possible in some fashion?

I'm trying to create a settings object, and pass it around my forms in project.

Ultimate goal is to have my application title, which will appear on all forms title text, in app.config or user.config.

Program.cs

namespace TestApp
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Properties.Settings settings = new Properties.Settings();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1(settings));
        }


    }
}

Form1.cs

namespace TestApp
{
    public partial class Form1 : Form
    {
        public Properties.Settings settings;

        public Form1(Properties.Settings Settings)
        {
            settings = Settings;
            InitializeComponent();
        }

        // use settings in other methods.
    }
}

Form1.Designer.cs

namespace TestApp
{
    partial class Form1
    {

        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }


        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {

            // usual code
            // 
            // Form1
            // 
            this.Text = settings.AppTitle; // form title using app settings
            //
            // usual code
            // 
        }
    }
}

The 2 errors I get are the same and point to the following bold objects in Form1.cs

(edit) bold not working?
public Properties.Settings **settings**;
(settings)

public **Form1**(Properties.Settings Settings)
(Form1)

The error is

error CS0052: Inconsistent accessibility: field type 'TestApp.Properties.Settings' is less accessible than field 'TestApp.Form1.settings'

I have searched the error, but those I found did not seem to relate to my problem and I do not really understand MS generic explanation.

I'm looking (a lot lately) for some education regarding this.

I appreciate you reading my post.

Edited 1 Year Ago by Suzie999

I have rethought my approach, and went about this a different way.
Created a static class.

namespace TestApp
{
    static class Global
    {
        public static Properties.Settings settings = new Properties.Settings();
    }
}

And all my forms get their title in InitializeComponent() like this.

// 
// Form1
// 
this.Text = Global.settings.AppTitle;

// 
// Form2
// 
this.Text = Global.settings.AppTitle + ": Configuration";

// 
// Form3
// 
this.Text = Global.settings.AppTitle + ": About";

etc...

I am still curious to know how I might achieve my original goal, or why it would not work, but not a priority any more.

I would also be interested in hearing any comments on how I resolved this, why it is bad, or how it might better be done etc..

Once again I thank you for looking in.

Edited 1 Year Ago by Suzie999

I mostly get that error message if something was private while it ought to be public. Remember that if you don't set an access specifier it is private by default.

Hi Suzy - As I read your second sentence I thought this needs to be a Singleton and was all geared up to tell you to scrap your original idea and use a Singleton. It seems that you pretty much solved this problem yourself though which is great!

Object orientated design patterns are solutions to common problems which someone else has already solved - often a long time ago and often they have been used and tested and validated for a long time by many developers.

One of the design patterns is a Singleton.

Although yours isn't quite the defined standard, it is indeed a Singleton. In fact there are many ways of coding a singleton which have pros and cons.

If you are serious about C# then design patterns are an absolute must. There are two really famous books on them which are:

http://books.google.co.uk/books/about/Design_Patterns.html?id=6oHuKQe3TjQC&redir_esc=y

http://shop.oreilly.com/product/9780596007126.do

This site is also good:

http://www.dofactory.com/net/design-patterns

And this guy does amazing videos on design patterns but they are in Java. Its easy enough to follow though as its a very simmilar language and its more about how you create objects and make them work together:

https://www.youtube.com/watch?v=vNHpsC5ng_E&list=PLF206E906175C7E07

Hope that helps!

ddanbe
That is why I thought all the discussions I found were unrelated.
Although in main() I got some errors (cannot remember which) when I tried to set an acces specifier to Properties.Settings settings = new Properties.Settings();

I also tried making it a field in Program class, and various other things, each leading to the same or other errors.

DaveAmour
Funny you should mention singleton, as I encountered a new problem not long after resolving this.

I seen a first chanve exception error in my console output which did not appear to affect my program.

Went into Debug, Exceptions, Common Runtime System, SystemArgumentException and checked the thrown checkbox, thinking it would throw an error and I could see where it was coming from. It did not, but I noticed that 2 instances of my app appeard to come up in my system tray.

Not the kind that are left behind from the last run when they're not cleaned up properly, both working and displying notifyIcon1 in the balloontip, and both allowing me to invoke my about and config dialog, although only one instance of my form was present, and only one instance of TestApp.vshost.exe in task manager.

Went back to the debug settings from above, and reset them, but it did not help, and I'm left with the two icons in tray each time I debug my application.

I have since, cleaned, rebuilt solution and restarted comp, and the problem persists.

Perhaps that might be for another thread though, if I cannot find the problem.

Anyway, thank you guys, for your help and input, It's very welcome and much appreciated.

Edited 1 Year Ago by Suzie999

This article has been dead for over six months. Start a new discussion instead.