Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8990927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:36:43+00:00 2026-06-15T22:36:43+00:00

I use the following dedicated class to manage session variables in my applications (ignore

  • 0

I use the following dedicated class to manage session variables in my applications (ignore misspelled names; that’s intentional):

This first code block is for usage. You can see the class it is calling on, at the next code block

//'importing' the class for current project
using SeSn = Debug_Tests.Seseions.SeSn;

// creating an object (usually with name related to currentProject)
public static SeSn.CreatCurrentSesionVariablsStructNamed CurSesVarStruct = new Seseions.SeSn.CreatCurrentSesionVariablsStructNamed();

// the long name helps me in this little 'chaos'

This is an instance of a struct, that is ‘grouping’ or ‘tying’ all my globals into one bundle. Whenever I may need to store my global variables, I will assign the values into the appropriate-struct-variable that CurSesVarStruct has to offer.

Then all I need is to access session variables once, only to extract the "Variable-collection" –object, … as it is actually a session variable that I keep its name constant – _CurrentSesionGlobals.

In short, it’s the struct that is stored in the session as one of the session variables – data type = object, or you could say a clone of the struct to be saved between sessions.

Since I have that and can use it with _CurrentSesionGlobals, I could just access any value I need from session through the following, for example:

Assign the struct before storing it in Session:

CurSesVarStruct.SelectedUercustid = custID;

Then the next method – ExtrctSesnVar() below, allows me to use for example:

Extract a variable that was saved in last session:

custID = ExtractSesnVar().SelectedUercustid;

So SelectedUercustid is actually one of the struct members.

The Question/Problem

Performing extraction of _CurrentSesionGlobals out of the session variables.

public static SeSn.CreatCurrentSesionVariablsStructNamed ExtrctSesnVar()
{
    var CurrAppGlobals = SeSn.GetValueAS.ACloneOfTheStructObj("_CurrentSesionGlobals");
    return (SeSn.CreatCurrentSesionVariablsStructNamed)CurrAppGlobals;
  //the question is refereing this location.
}

How can I have a return value for a null result, or a condition that will first ask if the object / a given Session Variable, that I am trying to extract isn’t null, or does not exist?

Currently there’s an exception error while I am trying to get the value of a non-existing session variable.

The next code block is a class that I add into the solution, as a helper to every website application. It’s actually a namespace, so the class that is responsible to handle session variables is Sesn:

namespace Seseions {
    public class Sesn {
        public static bool isNotEmpty() {
            return HttpContext.Current.Session.Keys.Count > 0;
        }
        public struct CreatCurrentSesionVariablsStructNamed {

            // some of commonly used variables- still testing options..

            public int ManagerCustID;
            public int SelectedUercustid;
            public int recordID;
            public int SelectedMonth;
            public int SelectedChosenWorker;
            public int SelectedYear ;

            
            public string SelectedTable;
            public string SelectedColumn;
            public string SqlSelectCommandLastQuery;
            public string TableOfUsersReference;
            public List<string> Fontlist { get; set; }

        }

        // converts and extract values of session variables

        public class GetValueAS {
            public static CreatCurrentSesionVariablsStructNamed ACloneOfTheStructObj(string currntProjectSesVarStructName) {
                if(HttpContext.Current.Session[currntProjectSesVarStructName] != null) {
                    return (CreatCurrentSesionVariablsStructNamed)HttpContext.Current.Session[currntProjectSesVarStructName]; 
            }

            public static int _Int(string SesParameterValToReturn) {
                return Convert.ToInt32(HttpContext.Current.Session[SesParameterValToReturn]);
            }

            public static string _String(string SesParameterValToReturn) {
                return Convert.ToString(HttpContext.Current.Session[SesParameterValToReturn]);
            }
            public static DataSet _DataSet(string SesParameterValToReturn) {
                return (DataSet)HttpContext.Current.Session[SesParameterValToReturn];
            }
            public static DataTable _DataTable(string SesParameterValToReturn) {
                return (DataTable)HttpContext.Current.Session[SesParameterValToReturn];
            }
            public static bool _Bool(string SeSnVarToCheckOn) {
                if (HttpContext.Current.Session[SeSnVarToCheckOn] == null)
                    return false;
                return (bool)HttpContext.Current.Session[SeSnVarToCheckOn];
            }
        }

        // an easy way to access and mange session variables actions
      public enum Act {
          Add, Remove, Replace
      }

      public static void Modify(Act action, string New_SesnVarName= null, object NewP_Value=null, string Currnt_Ses_SesnVarName=null) {
          switch (action) {
              case Act.Remove:
                  if (isNotEmpty()) {
                      HttpContext.Current.Session.Remove(CurSes_ParamName);
                  }
                  break;
              case Act.Replace:
                  HttpContext.Current.Session.Remove(CurSes_ParamName);
                  HttpContext.Current.Session.Add(New_SesnVarName, NewP_Value);
                  break;

              case Act.Add:
                  HttpContext.Current.Session.Add(NewQs_SesnVarName, NewP_Value);
                  break;
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T22:36:45+00:00Added an answer on June 15, 2026 at 10:36 pm

    Just don’t do this.

    • critical: do not put Session (user) related data in static variables. It is not thread-safe.
    • best practice: try to avoid static in ASP.NET for everything else too.
    • best practice: do not use structs for anything but small, immutable and identity-less types

    It seems you are over-engineering this. All you need (for now) is to use some constants for the strings:

    public static class SessionKeys
    {
       public const string ManagerCustID = "ManagerCustID";
       ...
    }
    

    and then you can start focusing on code that adds value to your app.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I see that Flex3 has a class called ApplicationControlBar dedicated to holding components that
I use following class to encrypt/decrypt my texts. http://code.google.com/p/iphonebits/source/browse/trunk/src/Encryption/NSData-AES.m?r=2 This works perfectly. But when
I use following method to get var from another class. However, firstView receives always
I use following style attribute so when i will start typing in text box
I use following code: Create a retry policy, when error, retry after 1 second,
I just use following code to add an image to my project, var paper
When i use following: var child:DisplayObjectContainer = dso.getChildAt( dso.numChildren -1 ) as DisplayObjectContainer; trace(child);
for example i use following command to find a record SELECT `users`.`mail` FROM `users`
I would like to use following sql to avoid constructing sql dynamically: SELECT CommentID,
I use VS2010, C# to develop Silverlight 4 app, I use following code in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.