Bug in DirectCast() ?

G

Guido Kraus

I tried this VB.NET 2003 (.Net runtime 1.1):

Dim o As Object = DirectCast(Nothing, HashTable)
If IsNothing(o) Then
Debug.WriteLine("Nothing")
Else
Debug.WriteLine("Something")
End If

Guess what: o is not Nothing!
Is this a bug or a feature? By the way: CType(Nothing, HashTable) returns
Nothing as I would expect.

Guido
 
B

Bill McCarthy

hi Guido,

recheck your code. I tested it and cannot reproduce. I get o is Nothing.


Bill.
 
H

Herfried K. Wagner [MVP]

* "Guido Kraus said:
I tried this VB.NET 2003 (.Net runtime 1.1):

Dim o As Object = DirectCast(Nothing, HashTable)
If IsNothing(o) Then
Debug.WriteLine("Nothing")
Else
Debug.WriteLine("Something")
End If

Guess what: o is not Nothing!
Is this a bug or a feature? By the way: CType(Nothing, HashTable) returns
Nothing as I would expect.

I am not able to repro that in .NET 1.1, VB.NET 2003.
 
B

Bill McCarthy

Hi Guido,

Yeh, I get the same in the watch window, but I still get the result to be
Nothing when actually run:
e.g:

Console.Writeline( DirectCast(Nothing, Hashtable) Is Nothing)

returns True.

What does your watch window say for :
DirectCast(Nothing, Hashtable) Is Nothing
 
C

Cor

Hi Guido,
Dim o As Object = DirectCast(Nothing, Hashtable)
Dim m As Object = CType(Nothing, Hashtable)
If m Is o Then
MessageBox.Show("both the same")
End If

both the same is showed (and they are nothing)

Cor
 
G

Guido Kraus

Hi Bill,

you are right.
DirectCast(Nothing, Hashtable) Is Nothing correctly returns Nothing and the
example I gave also works correctly. However, the watch window and the
variable tooltips obviously have a problem because they display {Length = 0}
instead of Nothing.

Guido
 
H

Herfried K. Wagner [MVP]

* "Guido Kraus said:
you are right.
DirectCast(Nothing, Hashtable) Is Nothing correctly returns Nothing and the
example I gave also works correctly. However, the watch window and the
variable tooltips obviously have a problem because they display {Length = 0}
instead of Nothing.

Yep. The watch window and the tooltips don't always show the right
data.
 

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

Top