HOWTO: Typed Session Variables in ASP.NET

Using typed variables with the ASP.NET Session object.

Anything placed into the Session object is stored as a simple 'Object' type. The Session object does not directly support any type checking/enforcement. While this is convenient for quickly slopping together some code, it tends to introduce problems in complex applications. Having each variable declared as a certain type saves you from making mindless mistakes during a long coding session, as well as it is a great reference to have each variable declared, along with comments describing each declared variable. Any time you have a lapse in remembering a detail about the data you are looking at, you can just quickly review the declarations for a quick refresher.

However, with the Session object, you do not declare your variables, you just throw them in there and hope you remember everything. (Sure, you can enumerate everything stored in the Session object at a certain time in your programs execution, but it's a bit clunky.)

After this driving me nuts for a while, I finally came up with a way to use typed variables with the Session object. It's not perfect, as the Autosense functionality of Visual Studio does not 'see' the variables declared with this method, so you only get the autocomplete functionality available with the base Session object. But, it works, allows you to work with typed variables. Maybe a good Macro coder could hack something together that would add functionality for this to the autocomplete feature of Visual Studio, but that's not something I know a whole lot about, so I'll have to leave that as an exercise left to the reader. :)

OK, how is this done?

First, create a public class containing your variable declarations. An example follows:

Public Class GlobalVarDefinitions

' This class is instantiated in the Session_Start section of Global.asax and is
' added to the session object to allow usage of typed session variables.

' Access everything in this class as Session("Globals").MemberName

Public UserName as String ' Comments can go here
Public Counter as Integer
Public tblDataTypes as DataTable
Public DateJoined as DateTime

End Class

Next, in the Session_Start section of Global.asax you handle the instantiation of the class and store it into the Session Object:

    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the session is started

        ' Create new instantiation of GlobalVarDefinitions
        Dim GlobalVars As New GlobalVarDefinitions

        ' Store in Session object to allow use of typed session variables
        Session("Globals") = GlobalVars

    End Sub

Now, you can go ahead and used the typed Session variables in this fashion: (Just example code, not really anything functional, only to give you an idea of how to store and retrieve data to/from the typed Global variables.)

Session("Globals").UserName = TextBox1.Text
Session("Globals").Counter = 0
Session("Globals").DateJoined as Now

Label1.Text = Session("Globals").UserName

Comfortably Anonymous
8/18/2005 2:19:51 PM
This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available in our efforts to advance understanding of environmental, political, human rights, economic, democracy, scientific, and social justice issues, etc. We believe this constitutes a 'fair use' of any such copyrighted material as provided for in section 107 of the US Copyright Law. In accordance with Title 17 U.S.C. Section 107, the material on this site is distributed without profit to those who have expressed a prior interest in receiving the included information for research and educational purposes. For more information go to: . If you wish to use copyrighted material from this site for purposes of your own that go beyond 'fair use', you must obtain permission from the copyright owner.