Solved

Session Boolean Set Get Syntax?

Posted on 2012-04-11
3
261 Views
Last Modified: 2012-04-11
I'm always looking for ways to shorten code while still maintaining function.

Q. Is there any functional difference between the two examples concerning set/get boolean in a Session?

[store boolean value]
   bool IsLightOn = true;
   Session["IsLightOn"] = IsLightOn;  

[get boolean value]
   bool IsLightOn;  
   if (Session["IsLightOn"] != null)  
       IsLightOn = (bool)Session["IsLightOn"];

- or -

[store boolean value]
   Session["IsLightOn"] = true;  

[get boolean value]
   if (Session["IsLightOn"] != null)  
       bool IsLightOn = (bool)Session["IsLightOn"];
0
Comment
Question by:pointeman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 250 total points
ID: 37832405
No there is no difference, in both the cases bool is boxed at the time of entering into session and unboxed when it is retrieved.
The only difference is however you are using a variable to store bool to session in first case.

You should ideally declare a variable if you know you are going to use it more than once in you code....
In your case Session["Value"]=true would be good enough;
0
 
LVL 2

Expert Comment

by:yogsoft
ID: 37833037
Well said @BuggyCoder. Also memory allocation can be avoided for variable in this case.
0
 

Author Closing Comment

by:pointeman
ID: 37833167
Thx, just wanted another EE opinion...
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question