Checking for Session cookies

  • Thread starter Thread starter tshad
  • Start date Start date
T

tshad

I was looking at different ways of doing the same thing and at the moment
was looking at the use of "is" and "=", as I have at times found that I will
do something like

if something = 0

and will get an error that says I can't use "=" (or <>) in this case case I
must use "is" ( or not something is). I am trying to find out when this is
the case - when I can use the "=" and when I have to use "is.

For example, I have a small piece of code checking for the existance of a
session cookie:

if session("tom") = nothing then
trace.warn("Tom not there")
trace.warn("Len(session('Tom')) = " & Len(session("Tom")))
if len(session("Tom")) = 0 then
trace.warn("also got a hit on <> 0")
end if
end if


This can also be run as:

if session("tom") is nothing then
trace.warn("Tom not there")
trace.warn("Len(session('Tom')) = " & Len(session("Tom")))
if len(session("Tom")) = 0 then
trace.warn("also got a hit on <> 0")
end if
end if

So 'session("Tom") = nothing' and 'session("Tom") is nothing' as well as
'Len(session("Tom"))' are all equivalent.

Not sure if there is a best way or just whatever strikes you fancy.

I do know that in some cases you can't use the "=" and must use "is". I
just don't know why in some cases you can use both (or all 3 ways) and in
some cases you must us "is".

Can someone help me out on the difference?

Thanks,

Tom
 
There are 2 sorts of types if you program in an OO language : reference and
value types

Value types are most built in types like integer, string (I think), money,
etc and you can think of the variable as actualy holding the value.
A reference type is Nothing (null in c#) or contains a reference to the actual
value which is always a class.

If you use a reference type and you want to know whether it contains a value
you do this by asking " is nothing", ie you are asking if it contains a reference.

But VB.NEt isn't so strict and it allows things like

dim i as integer = 5
if i = nothing then ...

This is not stritly right as i is a value type. But VB.NET guessed what you
ment (but guessing is a bad idea for a compiler).

So to be short : if you are dealing with classes you have to use nothing.

To complicate things further structs are value types. I suggest you google
for "difference reference value type" to dig in deeper cause it's quit a
technical topic.

Let me know if you have any more questions..

Cheers,
Tom Pester
 

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