Skip to page content or Skip to Accesskey List.

Work

Main Page Content

Empty Nothing And Null How Do You Feel Today

Rated 3.94 (Ratings: 4)

Want more?

  • More articles in Code
 
Picture of sgd

Scott Dexter

Member info

User since: 26 Apr 1999

Articles written: 10

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.

The access keys for this page are: ALT (Control on a Mac) plus:

evolt.org Evolt.org is an all-volunteer resource for web developers made up of a discussion list, a browser archive, and member-submitted articles. This article is the property of its author, please do not redistribute or use elsewhere without checking with the author.