Click here to Skip to main content
15,885,914 members
Articles / Web Development / ASP.NET
Tip/Trick

No More Session Variable Misspellings

Rate me:
Please Sign up or sign in to vote.
4.82/5 (7 votes)
23 Jun 2011CPOL1 min read 35.5K   2   3
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 could 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:

C#
public enum A00SessionVars { A00Value1, A00Value2, ... and so on };

And refer to it like this in the code:

C#
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#
// 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
'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:

C#
// For C#
int    i = GetSessionVar(A00SessionVars.A00Value1, -1);
string s = GetSessionVar(A00SessionVars.A00Value2, "N/A");
VB
' 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)


Written By
Software Developer (Senior) Paddedwall Software
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... Pin
rajivsimplythebest18-Jul-11 16:14
rajivsimplythebest18-Jul-11 16:14 
GeneralReason for my vote of 4 A suggested way to handle Database q... Pin
manish.kungwani30-Jun-11 6:31
manish.kungwani30-Jun-11 6:31 
GeneralReason for my vote of 5 Excellent solution! I would recommen... Pin
DrABELL23-Jun-11 3:04
DrABELL23-Jun-11 3:04 

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

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