Casting arrays that works on watch and command window but not in code.

E

Enrique Bustamante

Casting arrays that works on watch and command window but not in code.

My application is casting arrays in a way it should work. To test if I
was doing something invalid, I wrote a test code that has similar
structure of the classes in my application. The test worked fine, the
casting I want to do must work. I compared the structure of the test
with the application to ensure they where similar (inheriting twice,
base class implements interface,…).

If I break the application at the line it is generating the cast
exception, I am able to do the following:

In the watch window: cast the array. Works as expected.
In the command window: cast the array, get first element and print the
value of a property. Works ok.

I do not understand what is going on. At this stage, I have one
question:

if something works in the command window and in the watch window
should it work in Code?

I am using in the command window:
? DirectCast(interfaceParameters, BaseClass())(0).AnyStringProperty.
works ok.

I am using in the watch window:
DirectCast(interfaceParameters, BaseClass()). works ok.


====Test code with the same structure of the Application Code========

Option Strict On

Interface IMainInterface : Sub MainImplement() : End Interface


Class BaseClass : Implements IMainInterface

Public Sub MainImplement() Implements IMainInterface.MainImplement

End Sub

End Class

Class FirstDerivedClass : Inherits BaseClass : End Class

Class SecondDerivedClass : Inherits FirstDerivedClass : End Class

Module Test2

Sub Main()

'array of derived classes (two levels of inheritance)
Dim Seconds() As SecondDerivedClass = _
New SecondDerivedClass() {New SecondDerivedClass}

SubWithInterfaceParameters(Seconds)

End Sub

Sub SubWithInterfaceParameters(ByVal interfaceParameters() As
IMainInterface)

‘this line is the one that generates casting exception
'in my application. It worksfine with this test.
'The casting works in watch and command windows
'in the application

Dim bases() As BaseClass = _
DirectCast(interfaceParameters, BaseClass())

End Sub

End Module
 
E

Enrique Bustamante

Cor Ligthert said:
Enrique,

You show us a working example, what is it that we can do for you?

Cor

Cor,

The question posted is:

if something works in the command window and in the watch window
should it work in Code? If not, why?

I am using in the command window:
? DirectCast(interfaceParameters, BaseClass())(0).AnyStringProperty.
works ok in the command window. Does not work at code.

I am using in the watch window:
DirectCast(interfaceParameters, BaseClass()). works ok in the watch
window. Does not work at code.

I copy-pasted the statements from Code to command and watch windows.

======================================================================

The working sample is too big to send. I will think how to make it
available. Assume for now that the test code I posted has the same
structure that the working code.
=======================================================================

To get out of the problem I am using array.copyTo instead of a
directcast as follows:

dim bases(interfaceParameters.length-1) as baseclass
interfaceParameters.CopyTo(bases,0)

then I use bases
 

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