I know about creating DLL's but I think this is simpler then that.
I have three forms (Login, Config, MainForm) that each one needs to access global variables that are set throughout the application and all three forums need to be able to read a global function.
To try and make it simple I have a config.xml file that holds the settings for the application. So instead of me inserting this function to read the config file I would like to call "ReadConfig" from any of the three forms and it would set the global variables for the application (Example: The Username).
Can someone help guide me?

1 answers
Well the configuration file only needs to be read once and the best time to do that, I would suggest, is in the Load eventhandler of the startup form.
The ReadConfig() method should then place the configuration variables in private static fields of the startup form class and expose them to the other classes using public readonly static properties.
Judging by your <a target="_new" href="http://www.csharpfriends.com/Forums/ShowPost.aspx?PostID=54272">other thread</a>, Login is your startup form and so you already have ReadConfig() in the right form and are calling it from the right place (Login_Load). So all you need now is some static fields/properties to store/expose the settings. For example:
public partial class Login : Form
{
private static string someSetting; // null by default
public static string SomeSetting
{
get { return someSetting;}
}
private void Login_Load(object sender, EventArgs e)
{
ReadConfig();
}
private bool ReadConfig()
{
if (IsConfigAvailable())
{
someSetting = someString; // read from config file
return true;
}
else
{
// etc
return false;
}
}
// other code
}
The setting can now be read from another form with a line like:
string someSetting = Login.SomeSetting;
As it's static, there's no need to bother getting a reference to the Login form instance.
If there are a lot of settings, you could create a separate class to store them all and then have just the one static property which returns an object of that class.
answered 8 months ago by:
11603
This post was imported from csharpfriends, if you have a similiar question please ask it again.
All previous members have been migrated, hope you enjoy the new platform!