Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Storing Variables for Global Use In Application

Status
Not open for further replies.

BDRichardson

Programmer
Jul 23, 2003
46
GB
I am having some trouble with a simple problem of storing variables within my program. I am retrieving settings from within an XML file, which is working fine. But I need to store the variables in my program for the duration of its execution. I figured I need to store them in a separate .cs (Class) file within the project, possibly as Attributes. Then use the Set and Get methods to control the values. I do want the values to be stored in a separate class of their own so that they can be controlled and used by methods in several other classes.

* Am I approaching the problem the correct way?
* If I do the following, will the values remain set, unless modified, until the program terminates?

// Start of Settings Class
using System;

namespace Settings
{
// Main class, which will have several sub-classes
internal class clsMain
{
// Sub-class, which is one of several classes within the Main class
internal class clsSetting : Attribute
{
protected double dValue;
public double DoubleValue
{
get{return dValue;}
set{dValue = value;}
}

protected string sValue;
public double StringValue
{
get{return sValue;}
set{sValue = value;}
}
}
}
}

// End of Settings Class


// Sample code used in different class, within a different .cs file in the project
private clsMain.clsSetting Setting = new clsMain.clsSetting();

// Store values in Settings
Setting.DoubleValue = 12345;
Setting.StringValue = "TestValue";

// Retrieve values from Settings
MessageBox.Show("DoubleValue = " + Setting.DoubleValue.ToString());
MessageBox.Show("StringValue = " + Setting.StringValue());
 
You mean "Singleton" ??

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top