Reputation: 2861
whats the best way to set up a global variable that I can access thoughout a .net site. Basically I want to use it as a yes/no type of flag that I can refer to from any page.
thanks
Upvotes: 2
Views: 2396
Reputation: 159
You can create a variable using Application Object
.
Add the following code to the Application_Start
event in the code-behind module for the Global.asax file:
Application("abc") = "hi"
Upvotes: 0
Reputation: 5177
yes as TakeMeAsAGuest said
make a class static have all you configuration values you want
public static class ProgramSettings
{
private static bool _flag = false;
public static bool Flag
{
get { return _flag; }
set { _flag = value; }
}
}
on application start set you properties as you want and read it from xml file or read it from web.config by adding in web.config
<appSettings>
<add key="flag" value="true" />
</appSettings>
and read it in application_start like
using System.Configuration;
ProgramSettings.Flag = ConfigurationManager.AppSettings["flag"]
My Regards
Upvotes: 0
Reputation: 101192
Since you say flag I would use a const
since it prevents the bool from being changed anywhere in the code:
public static class ApplicationFlags
{
public const bool MyFlag = false;
}
if (ApplicationFlags.MyFlag)
Reponse.Write("Ohh, my!");
You could also use static fields. Please note that static fields are not thread safe. If you change them in one thread (one http request) it will also be changed for all other threads.
Upvotes: 1
Reputation: 66398
One way that wasn't described in detail yet (although mentioned) is using AppSetting
value from the web.config
file.
First, in the web.config
file, under <configuration>
add the following:
<appSettings>
<add key="MyGlobalVar" value="true"/>
</appSettings>
The <appSettings>
section might already exist, in such case just add the <add ...
to it.
Second, to be able to read from it, add reference to System.Configuration
assembly in your project.
Finally to read the value from any page, have such code:
string myGlobalValue = System.Configuration.ConfigurationManager.AppSettings["MyGlobalVar"];
You can then change the value in the .config
file and saving it will make the web application "restart" as the pool will be recycled.
Upvotes: 3
Reputation: 12979
If you are wanting to store these per user and they may change periodically because the user performed some behavior then it may be best for you to use session variables.
VB:
Session("FirstName") = FirstNameTextBox.Text
Session("LastName") = LastNameTextBox.Text
C#:
Session["FirstName"] = FirstNameTextBox.Text;
Session["LastName"] = LastNameTextBox.Text;
You can store any reference or value types in the Session. Just remember that as long as these values persist, they are taking up RAM on the servers for any users that have these variables set.
If you want to store a global variable that is used for all users, then you could use global static variables.
If you already know the value and it's never going to change, then you could do something like:
static string ImportantData = @"Some String That Will Never Change";
Upvotes: 0
Reputation: 5523
You can use static keyword for that. Also i suggest you to check enumerations in c#
Upvotes: 1
Reputation: 995
its very simple question. you can create and set public static a bool variable anywhere you want. an example is:
public static class ProgramSettings
{
public static bool Flag = false;
}
but if you want to change flag value without compiling, you may use configuration settings. (web.config)
Upvotes: 1
Reputation: 11100
Generally, global variables are kind of frowned upon, but if it fits into you design choices, then you can use a static class, with static members.
Upvotes: 2