Array: "Cannot Convert to interface IList" - Why?

U

Urs Eichmann

VS.NET 2003:

The line
? CType(New String() {"abc"},IList)

typed in the Immediate window

gives an exception "Cannot convert to 'Interface IList'"

why is that? A string array does implement IList, so I should certainly
be able to cast, isn't it?

Urs
 
U

Urs Eichmann

No. according to help, the declaration of the Array class is as follows:
[Visual Basic]
<Serializable>
MustInherit Public Class Array
Implements ICloneable, IList, ICollection, IEnumerable
 
C

CJ Taylor

Hmm..

So I just did a simple test... Built a simple string array as done, and
found some interesting info...

So I declared my string as

Dim st(2) as String
st = new String() {1,2,3}

then if you execute this code, set a breakpoint (for watching) and do

st.gettype().getinterfaces

Length comes up with 0

However, If I check the runtime type its String[]

Basetype..

System.Array

How strange...

Can you try doing this two Urs? Run the getinterfaces and tell me if you
get a Length 0 array.

-CJ

Urs Eichmann said:
No. according to help, the declaration of the Array class is as follows:
[Visual Basic]
<Serializable>
MustInherit Public Class Array
Implements ICloneable, IList, ICollection, IEnumerable



CJ said:
I thought Arrays only implemented IEnumerator, not IList.
 
C

CJ Taylor

What if you Ctyped it to an array first, then to IList.

I know thats completly against most coding standards and how it should work,
just trying to find a way to make it work.

-CJ
Urs Eichmann said:
No. according to help, the declaration of the Array class is as follows:
[Visual Basic]
<Serializable>
MustInherit Public Class Array
Implements ICloneable, IList, ICollection, IEnumerable



CJ said:
I thought Arrays only implemented IEnumerator, not IList.
 
J

Jay B. Harlow [MVP - Outlook]

Urs,
typed in the Immediate window
Why in the Immediate window?

It works just fine in a source file:

Dim list As Ilist = DirectCast(New String() {"abc"},IList)
why is that?
It would appear the error is a side effect of the Immediate window.
A string array does implement IList, so I should certainly
be able to cast, isn't it?
Yes you can use the DirectCast operator to cast to it, or the CType operator
to convert to it. In this case conversion will simply do the cast.

Hope this helps
Jay
 
U

Urs Eichmann

Well it is not only in the immediate window, but also in the watch
window. The problem is this can lead to wrong assumptions when debugging
an application, escpecially for beginners.

Urs
 
J

Jay B. Harlow [MVP - Outlook]

Urs,
I'm sorry, you may have missed my point.

My understanding is all the windows of the VS.NET debugger (immediate,
command, watch, autos, locals) use a VS.NET specific expression evaluator (I
understand that it is shared across all languages). If you attempt to enter
valid C# or valid VB.NET those expressions may not work as you expect.
Considering that you could be debugging both a C# & VB.NET project at the
same time, which language should it choose?

This is similar to the expressions expected by DataTable.Compute &
DataColumn.Expression, the DataSet has its own expression syntax that does
not match VB.NET (nor C#) although it is close to VB.NET...


Rather then entering valid VB.NET in the immediate, command, watch, autos,
or locals windows (which you demonstrated has problems) enter source in a
source file and compile it. This way you are guaranteed that you are using
the VB.NET compiler on those expressions, rather then 'something else'...

I'm not finding a KB article on this, I will post something to Microsoft to
see if there is a KB article or other info that identifies this problem.

Hope this helps
Jay
 

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