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

Discussion in 'ASP .Net' started by VB Programmer, Aug 4, 2003.

  1. 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!
     
    VB Programmer, Aug 4, 2003
    #1
    1. Advertisements

  2. 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
     
    S. Justin Gengo, Aug 4, 2003
    #2
    1. Advertisements

  3. Thanks for the great response!

    So would you use session variables where you needed a "global" variable?
     
    VB Programmer, Aug 4, 2003
    #3
  4. 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.)
     
    VB Programmer, Aug 4, 2003
    #4
  5. It's created automatically.

    --
    S. Justin Gengo, MCP
    Web Developer

    Free code library at:
    www.aboutfortunate.com

    "Out of chaos comes order."
    Nietzche
     
    S. Justin Gengo, Aug 5, 2003
    #5
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.