Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create Global variables, ASP.NET, global asax

I have some data layer classes that are to be used very frequently almost on the whole site.

I was working on a windows application previously and i used to create its object in module (vb.net) but now i m working in C# and on ASP.NET.

Now i need to do same thing so that i need not create same object multiple times on every page.
I want to use something like using a global variable.

How can i do this?
Is it done by using global.asax
can i create my objects in global.asax

I am a new to asp.net, so try to give the syntax along with an explanation.

like image 416
Shantanu Gupta Avatar asked Nov 25 '09 15:11

Shantanu Gupta


People also ask

Can we declare global variable in global asax?

I use Global. asax to define global variables e.g. You can set them in whichever event suits your purpose: Application_Start, Application_BeginRequest etc. You can reference them throughout the application.

How do you make a global variable in C#?

In C# you cannot define true global variables (in the sense that they don't belong to any class). You can then retrieve the defined values anywhere in your code (provided it's part of the same namespace ): String code = Globals. CODE_PREFIX + value.

What is global asax in ASP.NET with example?

Global. asax is the asp.net application file. It is an optional file that handles events raised by ASP.NET or by HttpModules. Mostly used for application and session start/end events and for global error handling.

How do I create a global asax file in Visual Studio?

How to add global. asax file: Select Website >>Add New Item (or Project >> Add New Item if you're using the Visual Studio web project model) and choose the Global Application Class template.


3 Answers

You don't actually need to use the global.asax. You can make a class that exposes your objects as statics. This is probably the most simple way

public static class GlobalVariables {
    public static int GlobalCounter { get; set; }
}

You can also use the Application State or even the ASP.NET Cache because those are shared across all sessions.

However, If I were in this situation, I would use a framework like Spring.NET to manage all my Sington instances.

Here is a quick example of how you would get at your class instances using Spring.NET

//The context object holds references to all of your objects
//You can wrap this up in a helper method 
IApplicationContext ctx = ContextRegistry.GetContext();

//Get a global object from the context. The context knows about "MyGlobal"
//through a configuration file
var global = (MyClass)ctx.GetObject("MyGloblal");

//in a different page you can access the instance the same way
//as long as you have specified Singleton in your configuration

But really, the bigger question here is why do you need to use global variables? I am guessing you don't really need them and there might be a better big picture soluion for you.

like image 180
Bob Avatar answered Oct 01 '22 05:10

Bob


I would recomend you to use application state for this purpose.

like image 40
XpiritO Avatar answered Oct 01 '22 03:10

XpiritO


I'll skip over the "should" part about using global variables in .NET and show some code I'm working in now that uses Global.asax for some "global" variables. Here's some info from that file:

public class Global : System.Web.HttpApplication
{
        public enum InvestigationRole
        {
            Complainent,
            Respondent,
            Witness,
        }

        public static string Dog = "Boston Terrier";
}

So from the ASPX pages, you can access those members by opening up the static Global class like so:

protected void Page_Load(object sender, EventArgs e)
{
    string theDog = Global.Dog; 
    // copies "Boston Terrier" into the new string.

    Global.InvestigationRole thisRole = Global.InvestigationRole.Witness;
    // new instance of this enum.
}

Buyer beware though. There are better ways of treating the concept of "global variables" in the .NET world, but the above will at least get you a layer of abstraction above repeating the same strings in all your ASPX pages.

like image 33
Graham Avatar answered Oct 01 '22 03:10

Graham