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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Global Constants in ASP.NET 1

Status
Not open for further replies.

sand133

Programmer
Jun 26, 2004
103
GB
Hi guys I am embarking a system re-write of our application from asp to asp.net.

My question is how do i go about creating and using a global constants file that is available within my aspx files as well as the associated code behind files. I have had a look around and after discussing it with some people i dont want to go down the webconfig route. Any other suggestions

many thanks.
 
You can create a class file that holds your constants if you don't want to use the web.config file.

Jim
 
You could also use Application variables.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
i have created a class, what are my options so i can access the variables from my aspx and the associated code behind files bareing in mind that i have 80 aspx files. Can i use include?? please do provide a code example if possible.

many thanks again
 
Unless you need to frequently change the global variables at runtime, the proper way to do this really is to use web.config either through the AppSettings or a custom ConfigurationSection if you need more than key/value pairs.


Note that it isn't to say you can provide programmatic wrappers for accessing the configuration section values in a friendly way, it just gives you a more organized and powerful place to put constants.

Otherwise, just do something like this:

public static class GlobalConstants
{
private static int _myInt;

static GlobalConstants
{
MyInt = 5;
}

public static int MyInt
{
get{ return _myInt; }
set{ _myInt = value; }
}
}

Then in your aspx or code-behind, you'd register the namespace and reference the variable thusly:

int localInt = GlobalConstants.MyInt;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top