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

  • Thread starter Thread starter VB Programmer
  • Start date Start date
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!
 
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
 
Thanks for the great response!

So would you use session variables where you needed a "global" variable?
 
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.)
 
Back
Top