|
-
September 21st, 2001, 10:22 AM
#1
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
-
September 21st, 2001, 10:53 AM
#2
Re: isNothing ???
For a Collection try Foo.Count which should be zero if the variable Foo was not initialized.
-
September 21st, 2001, 10:54 AM
#3
Re: isNothing ???
You didn't try
If Foo is nothing then
...
...
-
September 21st, 2001, 11:01 AM
#4
Re: isNothing ???
After setting Foo to a new Collection, Foo is no longer set to Nothing and your test will fail.
-
September 21st, 2001, 11:01 AM
#5
Re: isNothing ???
Hi
You need to do this:
Dim Foo as Collection
If Foo is nothing then
' your code
End If
-
September 21st, 2001, 11:04 AM
#6
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.
-
September 21st, 2001, 11:12 AM
#7
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.
-
September 21st, 2001, 11:31 AM
#8
Re: isNothing ???
Hey
Who cares what the outcome is..use an Else or If Not
the question was how to test for it!
-
September 21st, 2001, 02:50 PM
#9
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
-
September 22nd, 2001, 09:20 AM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|