471,601 Members | 1,192 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,601 software developers and data experts.

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

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
Nov 21 '05 #1
2 2043
Enrique,

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

Cor
Nov 21 '05 #2
"Cor Ligthert" <no************@planet.nl> wrote in message news:<OT**************@TK2MSFTNGP15.phx.gbl>...
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
Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Stewart Stevens | last post: by
27 posts views Thread by Abdullah Kauchali | last post: by
3 posts views Thread by Barry Mossman | last post: by
6 posts views Thread by Carlo Marchesoni | last post: by
1 post views Thread by Robbie | last post: by
reply views Thread by MichaelMortimer | last post: by
reply views Thread by CCCYYYY | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.