Click to See Complete Forum and Search --> : isNothing ???
Jenda
September 21st, 2001, 10:22 AM
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
MKSa
September 21st, 2001, 10:53 AM
For a Collection try Foo.Count which should be zero if the variable Foo was not initialized.
shree
September 21st, 2001, 10:54 AM
You didn't try
If Foo is nothing then
...
...
MKSa
September 21st, 2001, 11:01 AM
After setting Foo to a new Collection, Foo is no longer set to Nothing and your test will fail.
awylie
September 21st, 2001, 11:01 AM
Hi
You need to do this:
Dim Foo as Collection
If Foo is nothing then
' your code
End If
awylie
September 21st, 2001, 11:04 AM
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.
MKSa
September 21st, 2001, 11:12 AM
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.
awylie
September 21st, 2001, 11:31 AM
Hey
Who cares what the outcome is..use an Else or If Not
the question was how to test for it!
John G Duffy
September 21st, 2001, 02:50 PM
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
Jenda
September 22nd, 2001, 09:20 AM
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 <199808071736.KAA12738@wall.org>
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.