I want to make my variable static or "global" - so the same effect as static in .NET; every session that accesses it gets the same result, and if one session modifies it it affects everyone else too.
How can I achieve this in Classic ASP?
To declare a static variable, we have to use the keyword static while in case of global variable, if the variable is declared outside the main function without any special keyword, the compiler interprets it as a global variable.
A static variable can be either a global or local variable. Both are created by preceding the variable declaration with the keyword static. A local static variable is a variable that can maintain its value from one function call to another and it will exist until the program ends.
However, you can make a global variable by creating a static class in a separate class file in your application. First, create a class called global in your application with the code given below. The class also has a static variable. Now, you can access it from anywhere in your forms after assigning a value to it.
If you want to have a variable that is accessible application wide, you can use the application object. Be sure to use Application.Lock/Unlock to prevent any problems.
Application.Lock
Application("MyVariable") = "SomeValue"
Application.Unlock
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