Thursday, March 29, 2012

Session Variable

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")On 7/20/2004 10:01 PM, William Gower wrote:

> 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...

--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET
On 7/20/2004 10:59 PM, Craig Deelsnyder wrote:

> On 7/20/2004 10:01 PM, William Gower wrote:
>> 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...
sorry, I meant Option Strict...

--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET
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

"William Gower" <w_gower@.hotmail.com> wrote in message news:<uh43d6sbEHA.2408@.tk2msftngp13.phx.gbl>...
> 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")

0 comments:

Post a Comment