Click to See Complete Forum and Search --> : how to create Global variable in project


prachi001
October 1st, 2008, 03:05 AM
Hello!

I am using ASP.NET 2.0 (Visual Studio 2005). There is one library having one class in which all my variables & functions are defined.

I am referring this class library in my asp.net project.

Now, I want to create an object of this class for every new session. So I will create this object in Session_Start() event in Global.asax. But, how will I access this object in entire project. for e.g.: in Default.aspx.vb and in other aspx.vb pages? How to make that class object which is defined in Session_Start() event global for entire project?

Thank You.

mmetzger
October 1st, 2008, 07:08 AM
Probably the easiest thing to do is place it in the Session object -


MyObject obj = new MyObject();
Session["myobj"] = obj;

// Later code / methods:

MyObject obj = (MyObject)Session["myobj"];

// Make sure to add some error handling if the session expired / etc.


Admittedly that's C#, but you should get the idea. Just make sure to properly handle the case of the session expiring and the return object being null.