Select Case with Nothing

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to test multiple variables to see if they have been assigned
nothing.

I tried:

Select Case Nothing

but seems you can't use Nothing like you use True/False.
How would I about this problem?
 
I'm not exactly sure if this is what you're looking for but take a look at
the code below.


Sub test()
Dim a As Object
Dim b As Object

Set a = New Collection
'Set b = New Collection

Select Case True
Case IsNothing(a)
MsgBox "a is nothing"
Case IsNothing(b)
MsgBox "b is nothing"
Case Else
MsgBox "No uninitialized objects!"
End Select

End Sub

Public Function IsNothing(o As Object) As Boolean
IsNothing = o Is Nothing
End Function
 
I am trying to test multiple variables to see if they have been assigned
nothing.

I tried:

Select Case Nothing

but seems you can't use Nothing like you use True/False.
How would I about this problem?

What is the data type of the variables you are trying to check? If they are
a numeric data type, VB automatically assigns 0 (in the appropriate data
type) to them at their creation, so you can't test whether a value has been
assigned to them or not. If they are Strings, then they start life as a
NullString and you can test for this using

If YourStringVariable = vbNullString Then...

If the are Variants, you can test if anything has been assigned to it using
the IsEmpty function, like this

If IsEmpty(YourVariantVariable) Then...

Rick
 
My variables are range variables.
I set them using something like:

Set DataRange = Range("a1", "c1")

I know they can be assigned nothing because I can use

If Not DataRange Is Nothing Then

to test each individual variable
 
Okay, so if I understand you correctly, you are trying to test whether
DataRange is pointing to a range or not. And, for some reason, you want to
use a Select Case instead of the If-Then test you already know how to do. I
don't think you can do it that way. Well, I guess you could do this...

Select Case True
Case DataRange Is Nothing
......
Case Else
......
End Select

but, personally, I hate using True that way... when you do, the Case
statements are really nothing more than If-Then-Else blocks, so you might as
well use If-Then-Else.

Rick
 
Back
Top