Solved

Session Boolean Set Get Syntax?

Posted on 2012-04-11
3
260 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
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 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