Question: Difference between strMyVar and Session("strMyVar")

V

VB Programmer

If I have a variable I want to share in my application what is the
difference between just declaring a variable (Dim strMyVar as String) and
using a session variable (Session("strMyVar"))?

When should I use a session variable and when should I just declare it like
normal?

Thanks in advance!
 
S

S. Justin Gengo

When a user signs on to your website a unique session id is created for
them. This session id is stored on their computer as a cookie. When you
place an object into a session variable that object remains in server memory
between trips to the client and may be "reclaimed" from one page to the
next.

When you declare a variable on a per page basis such as: Dim strMyVar as
String the object is only persisted in memory while the page is built and
then destroyed after the output is sent to the client.

You should use regular variables whenever an object does not need to persist
beyond a single request to the server. And use session variables when you
need to reference an object from one request to the server to another.

I hope this helps.

--
S. Justin Gengo, MCP
Web Developer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
 
V

VB Programmer

Thanks for the great response!

So would you use session variables where you needed a "global" variable?
 
V

VB Programmer

Thanks!

Is a cookie automatically created if I use session("xxx")? Or do I have to
enable something? (I do realize that the client has to have cookies enabled
on their end.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top