469,363 Members | 2,566 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,363 developers. It's quick & easy.

Is Len(xx) = 0 check same as IsNull check?

Hi all

In my functions I'm using a double-check all the time to trap if a value has
nothing in it and my question is do I need this double-check.

My check line is:

IF IsNull(xxx) OR LEN(xxx) = 0 THEN

blah blah

ELSE

blah blah

END IF

xxx is usually an array built up of data from an ADO recordset so will
LEN(xxx) = 0 cover my zero values or do I still need the IsNull check as
well?

Thanks

Nov 22 '05 #1
4 9916
Laphan wrote on 15 nov 2005 in microsoft.public.inetserver.asp.general:
IF IsNull(xxx) OR LEN(xxx) = 0 THEN
blah blah
ELSE
blah blah
END IF

xxx is usually an array built up of data from an ADO recordset so will
LEN(xxx) = 0 cover my zero values or do I still need the IsNull check
as well?


What is "the zero values" of an array?
NonExisting or
Empty or
filled with empty strings?

The length of a vbs array is found by ubound()
len() returns the character length of a string
[This is different in javascript, where .length does both]

What were your test results?

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Nov 22 '05 #2
Laphan wrote:
Hi all

In my functions I'm using a double-check all the time to trap if a
value has nothing in it and my question is do I need this
double-check.

My check line is:

IF IsNull(xxx) OR LEN(xxx) = 0 THEN

blah blah

ELSE

blah blah

END IF

xxx is usually an array built up of data from an ADO recordset so will
LEN(xxx) = 0 cover my zero values or do I still need the IsNull check
as well?

To answer the question in the subject line:
No
IsNull("") will return false
IsNull(Null) will return true
(len(Null)=0) will return Null
(len("")=0) will return true

However, you can shorten your statement by taking advantage of a strange
behavior in vb/vbscript: concatenating a string to a null will result in a
string, NOT in a null. This is not the expected behavior because most other
operations involving Null will result in Null (1 + null results in null).
So:
IF len(xxx & "") = 0 THEN
will serve your purpose

The answer may not be the same in other languages.

However, if xxx should be an array, then you need to use IsArray to test
whether or not it is an array. If you mean that you want to test a
particular element in the array, i.e., xxx(0), then yes, use len(xxx(0) &
"")=0. Only use IsNull if you want to specifically check for Null.

Bob Barrows

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Nov 22 '05 #3

"Laphan" <in**@SpamMeNot.com> wrote in message
news:ee**************@TK2MSFTNGP10.phx.gbl...
Hi all

In my functions I'm using a double-check all the time to trap if a value has nothing in it and my question is do I need this double-check.

My check line is:

IF IsNull(xxx) OR LEN(xxx) = 0 THEN

blah blah

ELSE

blah blah

END IF

xxx is usually an array built up of data from an ADO recordset so will
LEN(xxx) = 0 cover my zero values or do I still need the IsNull check as
well?


I believe the short answer to your opening question is, "No."

Null is an undefined value and is used to indicate that a variable contains
no valid data.

Empty is used to indicate an uninitialized variable but it is not the same
thing as Null.

"" is a value whose Len() is zero but is not the same thing as either Null
or Empty.

To cover the bases you might want to consider something like:

If IsNull(str) Or IsEmpty(str) Or Len(str) = 0 Then

There is also an IsNothing() but it applies to objects that were Set.
Nov 22 '05 #4
Many thanks guys

brilliant advice as always.

Rgds Laphan
"MyndPhlyp" <no****@homeright.now> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...

"Laphan" <in**@SpamMeNot.com> wrote in message
news:ee**************@TK2MSFTNGP10.phx.gbl...
Hi all

In my functions I'm using a double-check all the time to trap if a value has nothing in it and my question is do I need this double-check.

My check line is:

IF IsNull(xxx) OR LEN(xxx) = 0 THEN

blah blah

ELSE

blah blah

END IF

xxx is usually an array built up of data from an ADO recordset so will
LEN(xxx) = 0 cover my zero values or do I still need the IsNull check as
well?


I believe the short answer to your opening question is, "No."

Null is an undefined value and is used to indicate that a variable contains
no valid data.

Empty is used to indicate an uninitialized variable but it is not the same
thing as Null.

"" is a value whose Len() is zero but is not the same thing as either Null
or Empty.

To cover the bases you might want to consider something like:

If IsNull(str) Or IsEmpty(str) Or Len(str) = 0 Then

There is also an IsNothing() but it applies to objects that were Set.

Nov 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Tu Quach | last post: by
5 posts views Thread by Krechting | last post: by
4 posts views Thread by tlyczko | last post: by
5 posts views Thread by Andrew Meador | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.