Click here to Skip to main content
Click here to Skip to main content

Tagged as

No More Session Variable Misspellings

, 23 Jun 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
One way to guarantee uniqueness and avoid misspelling your session variable names
I was recently tasked with adding a series of web pages to a huge existing site. Since I was new to the project, I wasn't privvy to all of the previously existing session variables, presenting quite the problem where naming a new Session element was concerned. First, I gave all of my new session variables a prefix so I culd at least guarantee (to some extent) that they would be unique to the module I was working on. However, the list of variables became extensive, not to mention the possibility of typing a name incorrectly, thus unintentionally creating another variable. To solve this issue, I came up with the following scheme.
 
In a word, it involves using an enum. I simply define an enum, like so:
 
public enum A00SessionVars { A00Value1, A00Value2, ... and so on };
 
And refer to it like this in the code:
 
Session(A00SessionVars.A00Value1.ToString())
 
Yes, it made for more typing, but I never once had anything unexpected happen as a result of spelling a name wrong, and it's impossible to add two variables that are the same name (with a deft bow of the head in the direction of C#'s case-sensitivity).
 
EDIT ==============
 
I was working on the code again today, and decided that using ToString() was too clunky, so I came up with this:
 
// c# version
//-----------------------------------------------------------------
Public T GetSessionVar<T>(A00SessionVars item, T defaultValue)
{
    T value = defaultValue;
    Object obj = Session(item.ToString();
    if (obj == null)
    {
        SetSessionVar(item.ToString(), value);
    }
    else
    {
        value = obj as T;
    }
    return value;
}
 
//-----------------------------------------------------------------
Public void SetSessionVar(A00SessionVars item, Object value)
{
    Session(item.ToString()) = value;
}
 
'VB version
'-------------------------------------------------------------------------------------
Public Function GetSessionVar(Of T)(ByVal item As G00SessionVars, ByVal defaultValue As T) As T
    Dim value As T = defaultValue
    Dim obj As Object = Session(item.ToString())
    If (obj Is Nothing) Then
        SetSessionVar(item.ToString(), value)
    Else
        value = CType(obj, T)
    End If
    Return value
End Function
 
'-------------------------------------------------------------------------------------
Public Sub SetSessionVar(ByVal item As G00SessionVars, ByVal value As Object)
    Session(item.ToString()) = value
End Sub
 
Now I can get/set just by doing this:
 
// For C#
int    i = GetSessionVar(A00SessionVars.A00Value1, -1);
string s = GetSessionVar(A00SessionVars.A00Value2, "N/A");
 
' For VB
Dim i As Integer = GetSessionVar(A00SessionVars.A00Value1, -1)
Dim s As String  = GetSessionVar(A00SessionVars.A00Value2, "N/A")
 
Now, I'm going to go back and see if anyone suggested this (I haven't read any of the alternatives or comments yet).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

John Simmons / outlaw programmer
Software Developer (Senior)
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.
 
My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Comments and Discussions

 
Generalya that's nice dear this is the unique coding and very helpf... Pinmemberrajivsimplythebest18-Jul-11 17:14 
GeneralReason for my vote of 4 A suggested way to handle Database q... Pinmembermanish.kungwani30-Jun-11 7:31 
GeneralReason for my vote of 5 Excellent solution! I would recommen... PinmemberDrABELL23-Jun-11 4:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150129.1 | Last Updated 23 Jun 2011
Article Copyright 2011 by John Simmons / outlaw programmer
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid