Accessing a Program member from within a Form1 class

B

bg_ie

Hi,

I have my own UserSettings class that I wish to include within my
windows application. To include it at the moment, I do something like
this -

namespace WindowsApplication1
{
static class Program
{
static public UserSettings userSettings = new UserSettings();

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.ApplicationExit += OnApplicationExit;

Application.Run(new Form1());
}

static void OnApplicationExit(object sender, EventArgs e)
{
XmlSerializer s = new XmlSerializer(typeof(UserSettings));
TextWriter w = new StreamWriter(@"c:\settings.xml");
s.Serialize(w, userSettings);
w.Close();
}
}
}

But to access its members from within Form1 I have to do the following
-

WindowsApplication1.Program.userSettings.Version = "P-100928"

This works fine, but it doesn't feel like a good approach.

I declared userSettings within Program because I need to serialise it
on program exit, as shown above.

Any suggestions as to how I might do this better?

Thanks,

Barry.
 
D

DeveloperX

Hi,

I have my own UserSettings class that I wish to include within my
windows application. To include it at the moment, I do something like
this -

namespace WindowsApplication1
{
static class Program
{
static public UserSettings userSettings = new UserSettings();

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.ApplicationExit += OnApplicationExit;

Application.Run(new Form1());
}

static void OnApplicationExit(object sender, EventArgs e)
{
XmlSerializer s = new XmlSerializer(typeof(UserSettings));
TextWriter w = new StreamWriter(@"c:\settings.xml");
s.Serialize(w, userSettings);
w.Close();
}
}

}

But to access its members from within Form1 I have to do the following
-

WindowsApplication1.Program.userSettings.Version = "P-100928"

This works fine, but it doesn't feel like a good approach.

I declared userSettings within Program because I need to serialise it
on program exit, as shown above.

Any suggestions as to how I might do this better?

Thanks,

Barry.

Hi, you could create a static class which holds your settings as a
singleton which could then be serialized then you could access your
settings anywhere using UserSettings.Settings.ASetting

You would need to make the UserSettings class Static or implement a
static property, which would expose a single instance of a real object
(I've called the property Settings in the above).
The reason for this is you can't serialize a static class, so we wrap
the settings up in a real object that could be serialized.

Something like this really bare bones implementation.

public class MySettings
{
private static Settings _settings;

public static Settings Settings
{
get
{
if (_settings == null)
{
_settings = new Settings();
}
return _settings;
}
}
}
[Serializable]
public class Settings : System.Runtime.Serialization.ISerializable
{
public string ASetting="I am a setting";
public void
GetObjectData(System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context)
{
// TODO: Add Settings.GetObjectData implementation
}
}




Then to use:


Settings s = MySettings.Settings; //don't need to do this, you can
just do:
Console.WriteLine(MySettings.Settings.ASetting);

As an alternative, if you're framework 1.1, look at the enterprise
library configuration block, and in 2.0 there's a configuration
manager built in.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top