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

Discussion in 'ASP General' started by Laphan, Nov 15, 2005.

  1. Laphan

    Laphan Guest

    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
     
    Laphan, Nov 15, 2005
    #1
    1. Advertising

  2. Laphan

    Evertjan. Guest

    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)
     
    Evertjan., Nov 15, 2005
    #2
    1. Advertising

  3. 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.
     
    Bob Barrows [MVP], Nov 15, 2005
    #3
  4. Laphan

    MyndPhlyp Guest

    "Laphan" <> wrote in message
    news:...
    > 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.
     
    MyndPhlyp, Nov 15, 2005
    #4
  5. Laphan

    Laphan Guest

    Many thanks guys

    brilliant advice as always.

    Rgds Laphan


    "MyndPhlyp" <> wrote in message
    news:%23rB$...

    "Laphan" <> wrote in message
    news:...
    > 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.
     
    Laphan, Nov 15, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. EDOnLine

    IsNull Test ???

    EDOnLine, Dec 3, 2003, in forum: ASP .Net
    Replies:
    5
    Views:
    1,338
    mikeb
    Dec 4, 2003
  2. D. Shane Fowlkes

    Is IsNull supported in ASP.NET?

    D. Shane Fowlkes, Dec 18, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    25,412
    D. Shane Fowlkes
    Dec 18, 2003
  3. Brent

    SQLServer IsNull() & DataReader

    Brent, Apr 29, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,394
    Cowboy \(Gregory A. Beamer\)
    Apr 29, 2004
  4. Tor Erik Soenvisen
    Replies:
    14
    Views:
    594
    Tim Roberts
    Nov 23, 2006
  5. maestro
    Replies:
    1
    Views:
    324
    Chris
    Aug 11, 2008
Loading...

Share This Page