Session Variable

  • Thread starter Thread starter William Gower
  • Start date Start date
W

William Gower

If I have a Session variable such as Session("Id") which is a long, do I
have to convert it before assigning it to a long variable?

Dim nId As Long
nId = Session("Id")
 
If I have a Session variable such as Session("Id") which is a long, do I
have to convert it before assigning it to a long variable?

Dim nId As Long
nId = Session("Id")

Only if you have Option Explicit on (which I highly recommend; see the
Properties of your project)....otherwise at runtime it will convert it
and if it's invalid, you'll get a format exception...
 
Only if you have Option Explicit on (which I highly recommend; see the
Properties of your project)....otherwise at runtime it will convert it
and if it's invalid, you'll get a format exception...
sorry, I meant Option Strict....
 
Yes, at least if you have Option Strict on (which you should). If you
know in advance that the Session variable holds a Long (and never
anything else), you can use DirectCast to maximize performance:

Dim nId As Long
If Session("Id") Is Nothing Then
' Session has expired, or session variable was never set.
Else
nId = DirectCast(Session("Id"), Long)
End If
 

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

Back
Top