Solved

Session Boolean Set Get Syntax?

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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 …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now