Solved

Session Boolean Set Get Syntax?

Posted on 2012-04-11
3
264 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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

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 …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

623 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