-
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
-
Re: isNothing ???
For a Collection try Foo.Count which should be zero if the variable Foo was not initialized.
-
Re: isNothing ???
You didn't try
If Foo is nothing then
...
...
-
Re: isNothing ???
After setting Foo to a new Collection, Foo is no longer set to Nothing and your test will fail.
-
Re: isNothing ???
Hi
You need to do this:
Dim Foo as Collection
If Foo is nothing then
' your code
End If
-
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.
-
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.
-
Re: isNothing ???
Hey
Who cares what the outcome is..use an Else or If Not
the question was how to test for it!
-
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
-
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]>