473,388 Members | 1,073 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 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 2147
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Stewart Stevens | last post by:
I have a structure that looks like this: public __value struct mytype { float first; float second; }; I am trying to pass arrays of these things over a socket. So I have:
27
by: Abdullah Kauchali | last post by:
Hi folks, Can one rely on the order of keys inserted into an associative Javascript array? For example: var o = new Object(); o = "Adam"; o = "Eve";
3
by: Barry Mossman | last post by:
Hi, I get the feeling that I am missing something with regards to casting. The CopyTo method allows me to copy the contents of a collection into an array. My collection is a MatchCollection...
6
by: Carlo Marchesoni | last post by:
I have an ASP.NET/C# solution, where I can perfectly cast something I stored in the session object to a class of mine (BackEnd), as this: ->be = (BackEnd)Session;<- But if I try to do the same:...
1
by: m830266 | last post by:
I'm trying to use the APAX serial I/O control (www.turbocontrol.com/AProZilla.htm) in a VB.NET project and I'm having trouble with its 'data received' events. The data is supplied by APAX as a...
6
by: Tau | last post by:
look my simple code below(vc8): double ends; ends=8.3; when i debug it, the variable ends cannot be displayed in the watch window. it just said "error: index '0' out of bound for...
1
by: Robbie | last post by:
I use Visual C++ 6.0. I can watch small two-dimensional arrays by using something like myarray,10 myarray,10 .. .. .. myarray,10
5
by: lmawler | last post by:
Hi, I am executing sql from VB to populate a form... (I'm building a search, so my code takes what the user inputs, queries the database, and then populates a table, to which the fields on my form...
15
geolemon
by: geolemon | last post by:
I'm having a seriously Twilight Zone moment: In the code below, I'm building a SQL command into a string variable, declared at the top of my code. Then, I connect to the database and try to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.