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

L

Laphan

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
 
E

Evertjan.

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?
 
B

Bob Barrows [MVP]

Laphan said:
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
 
M

MyndPhlyp

Laphan said:
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.
 
L

Laphan

Many thanks guys

brilliant advice as always.

Rgds Laphan



Laphan said:
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.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top