I'm searching for a way to create a global constant variable, which I can use in my controllers.
I've totally no idea how to create that.
Thanks in advance
To get started in adding your static global variables in ASP.NET, create the App_Code folder. Then: Right-click on your project name and select "Add ASP.NET Folder" and then App_Code. Here is the code you can put in the class file. Static: The Global class is static.
Global: Static fields and properties in ASP.NET are always global. Another copy is never created. You will not have duplication. Performance: Static fields are efficient in normal situations.
We use static properties (in the C# language) to store this single instance data. First, older versions of ASP used the Application [] object, but this is inefficient in ASP.NET. To get started in adding your static global variables in ASP.NET, create the App_Code folder.
The "get" and "set" keywords mean you can access the field variable through the property. Example 2. Here we want to use the static global variables in the class. Open your web page's code-behind file. It is usually named Default.aspx.cs. This web form accesses the global variable. And Page_Load first gets the static data.
Very comfortable way is to use Strongly-Typed Settings for that. You can access to these variables everywhere in a project and change its values without recompilation.
You can use Visual Studio editor to define settings (Project > Properties > Settings):
These variables will be added to an appropriate section in a Web.config or App.config file in this way:
<setting name="SomeStringVariable" serializeAs="String">
<value>SomeStringValue</value>
</setting>
<setting name="SomeBoolVariable" serializeAs="String">
<value>false</value>
</setting>
<setting name="SomeDoubleVariable" serializeAs="String">
<value>1.23</value>
</setting>
You can use defined variables anywhere in your project in a simple way:
string myStringVariable = Settings.Default.SomeStringVariable;
bool myBoolVarialbe = Settings.Default.SomeBoolVariable;
double myDoubleVariable = Settings.Default.SomeDoubleVariable;
1: generate a static class(say Constant.cs)
set the property as
public static string YourConstant{
get { return "YourConstantValue";}}
accesses it anywhere
Constant.YourConstant;
or 2. you can also use web.config
<appSettings><add key="YourConstant" value="YourConstantValue" /></appSettings>
Use it as
ConfigurationManager.AppSettings["YourConstant"];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With