CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: isNothing ???

  1. #1
    Join Date
    Sep 2001
    Location
    Prague, Czech Republic
    Posts
    43

    isNothing ???

    If I have a variable declared like this :
    Dim Foo as Collection



    How do I find out whether the variable was initialized to anything either by set Foo = new Collection

    or by set Foo = Bar



    I've tried isEmpty(), isObject(), isNull(), If Foo = Nothing Then.
    The first three return the same for initialized and not initialized object, the last one leads to an error.

    For now I've changed the declaration to
    Dim Foo as Variant


    since then the isEmpty() works, but I don't think this is the best way.

    Thanks, Jenda


  2. #2
    Join Date
    Sep 2001
    Location
    IL, USA
    Posts
    1,090

    Re: isNothing ???

    For a Collection try Foo.Count which should be zero if the variable Foo was not initialized.


  3. #3
    Join Date
    Mar 1999
    Location
    Nepal
    Posts
    540

    Re: isNothing ???

    You didn't try


    If Foo is nothing then
    ...
    ...







  4. #4
    Join Date
    Sep 2001
    Location
    IL, USA
    Posts
    1,090

    Re: isNothing ???

    After setting Foo to a new Collection, Foo is no longer set to Nothing and your test will fail.


  5. #5
    Join Date
    Aug 2000
    Location
    Dublin
    Posts
    20

    Re: isNothing ???

    Hi

    You need to do this:


    Dim Foo as Collection

    If Foo is nothing then
    ' your code
    End If





  6. #6
    Join Date
    Aug 2000
    Location
    Dublin
    Posts
    20

    Re: isNothing ???

    If you try this and Foo wasn't set to a New Collection for some reason, you'll get an error message:

    Run-time error '91':
    Object variable or With block variable not set.


  7. #7
    Join Date
    Sep 2001
    Location
    IL, USA
    Posts
    1,090

    Re: isNothing ???

    True. But in the question Foo was set to New Collection. The Foo is Nothing test will be true after the Dim Foo as Collection

    statement but will fail after theset Foo=new Collection

    statement.


  8. #8
    Join Date
    Aug 2000
    Location
    Dublin
    Posts
    20

    Re: isNothing ???

    Hey

    Who cares what the outcome is..use an Else or If Not

    the question was how to test for it!


  9. #9
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: isNothing ???

    Just my two cents

    option Explicit

    Dim Foo as Collection

    private Sub Command1_Click()
    set Foo = new Collection
    If Foo is nothing then
    MsgBox "foo is nothing"
    else
    MsgBox "Foo is something"
    End If
    set Foo = nothing
    End Sub

    private Sub Command2_Click()
    If Foo is nothing then
    MsgBox "foo is nothing"
    else
    MsgBox "Foo is something"
    End If

    End Sub




    John G

  10. #10
    Join Date
    Sep 2001
    Location
    Prague, Czech Republic
    Posts
    43

    Re: isNothing ???

    In perlfunc manpage the author wrote about the described functions:
    "In general, they do what you want, unless you want consistency."
    I guess he did not know VB.

    Does anyone have any idea why do you have isEmpty(var) and "if var = Empty then", isNull(var) and "if var = Null then", but no isNothing and "if var is Nothing then" ???

    I can understand "@var IS NULL" in SQL, but this ???

    Jenda
    =============================
    : What do people think?
    What, do people think? :-)
    -- Larry Wall in <[email protected]>



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured