[thelist] okay, this one's real
Scott Dexter
sgd at ti3.com
Fri, 13 Aug 1999 18:44:18 -0500
<tip type="ASP,VBScript" Title="Empty, Nothing, and Null. How do you feel
today?">
Okay, lemme 'splain... no time, lemme sum up:
in VBScript there are three (yup, three) states of a variable not having a
value (well, a usable one anyway).
Empty == Uninitialized
Null == actually, well, Null. Okay, so technically this is a value. More in
a sec.
Nothing == for objects only, basically the same as Empty
"" --the empty string (VBScript calls it the null string) is a valid value.
Remember this.
VBScript also has three ways to check for non values: IsEmpty, IsNull, and
IsNothing. The lesson is that you can't mix and match. And none of them will
cooperate if you're checking against "". You check for "" by comparing
against "".
For those of you hellbent on the long answer, here you go:
If you set a variable to the empty string to clear it out, say
Session("userid")="", then VBScript still treats it as containing a value.
The corollary is that the storage is still allocated for it in memory-- no
garbage collection. If you try IsEmpty(Session("userid")) you'll get false.
IsNull(Session("userid")) returns false as well. IsNothing is either false
or an error, I don't remember.
So how do you clear it out? Do this instead: Session("userid") = Empty. Now
IsEmpty(Session("userid")) returns true. Memory used to hold the variable is
freed. IsNull(Session("userid")) still returns false, because its not Null.
IsNothing is the same as above.
"So what's Null?"
According to VB lore (documentation), Null is "no valid data." The only time
I've seen something actually return a value of Null is when pulling data
from a db. So if I have a recordset oRS with a field called firstname and it
is Null in the db, then IsNull(oRS("firstname")) will return True. And
IsEmpty? --False. You can set a variable to Null, but again, VBScript treats
this as some value (although not "valid data") and keeps the storage
allocated.
Oh, and then there's Nothing.
Nothing is the special trick to making sure your objects are shutdown,
closed out and deallocated. Anytime you do a Set myvar =... you want to Set
myvar=Nothing when you're done using it. Otherwise VBScript holds onto it,
the memory it takes up, and the information in it until you do or the ASP
page finishes processing. So yeah, you can get away without it, but you're
stuff will run faster if you do, and you'll be able to brag about how good a
programmer you are.
You can't set a regular variable to Nothing. You have to set it to
Empty. An object set to Nothing will return false if you try
IsEmpty(object). IsNull will return false as well.
"So why the hell all this cornfusing information?"
So you are more careful with your code, and maybe those times where you're
getting values or the page does something funky you now know why.
I'm trying to eliminate:
"Session("userid")="" ...why the hell is IsEmpty(Session("userid"))
returning false?!?!? #$@%@#*%!"
from your life, because I like you.
</tip>
sgd
--
think safely