If/EndIf and Select Case different outcomes.

  • Thread starter Thread starter Bob Day
  • Start date Start date
B

Bob Day

Why do the if/then and Select Case statements below results in different
outcomes?

Bob Day
Dim Test_Object As Object = 0

If Not (Test_Object Is Nothing) Then

' this considers Test_Object <> Nothing

Stop

End If

Select Case Test_Object

Case Is <> Nothing

' this considers Test_Object = Nothing

Stop

End Select
 
Bob Day said:
Why do the if/then and Select Case statements below results in different
outcomes?

Bob Day
Dim Test_Object As Object = 0

If Not (Test_Object Is Nothing) Then

' this considers Test_Object <> Nothing

Stop

End If

Select Case Test_Object

Case Is <> Nothing

' this considers Test_Object = Nothing

Stop

End Select

'Test_Object' doesn't contain a 'Nothing' reference, but it contains a
'Nothing' value!

\\\
MsgBox(Test_Object Is Nothing) ' False.
MsgBox(Test_Object = Nothing) ' True
///

For value types 'Nothing' will result in the data types default value (0 for
numeric data types). When using the '='/'<>' comparison operators, a value
comparison will be performed. 'Is' compares references.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top