-1

(before closing as duplicate, please read: I've looked through similar topic, but there is not enough explanation where the code should execute)

I want to set Properties.Settings.Default to a shorter variable while form is loaded..

    public Object xx;
    public Form1()
    {
        InitializeComponent();
        xx = Properties.Settings.Default;
    }
    private void button1_Click(object sender, EventArgs e)
    {
       MessageBox.Show(xx.something_name);
    }

I get errors later, while calling xx.something_name... How to do that successfully?

T.Todua
  • 53,146
  • 19
  • 236
  • 237

2 Answers2

4

Just store xx with the correct type, not as Object:

private Properties.Settings xx;
public Form1()
{
    InitializeComponent();
    xx = Properties.Settings.Default;
}
Marc
  • 3,905
  • 4
  • 21
  • 37
  • I get `Error CS0052 Inconsistent accessibility: field type 'Settings' is less accessible than field 'Form1.xx'` – T.Todua Jan 02 '17 at 21:52
  • I had to change that from `Public` to `Private`! Thanks! – T.Todua Jan 02 '17 at 21:58
  • ah sorry... by default, Settings classes are generated with an internal access modifier. You can change the generation, or you can declare the variable as private or internal. I have updated my answer. – Marc Jan 02 '17 at 21:59
3

You get an error as you are assigning to Object. Make xx of type Properties.Settings.

public Properties.Settings xx;
public Form1()
{
    InitializeComponent();
    xx = Properties.Settings.Default;
}
Paweł Łukasik
  • 3,893
  • 1
  • 24
  • 36