473,738 Members | 7,110 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(inte rfaceParameters , BaseClass())(0) .AnyStringPrope rty.
works ok.

I am using in the watch window:
DirectCast(inte rfaceParameters , 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 FirstDerivedCla ss : Inherits BaseClass : End Class

Class SecondDerivedCl ass : Inherits FirstDerivedCla ss : End Class

Module Test2

Sub Main()

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

SubWithInterfac eParameters(Sec onds)

End Sub

Sub SubWithInterfac eParameters(ByV al interfaceParame ters() 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(inte rfaceParameters , BaseClass())

End Sub

End Module
Nov 21 '05 #1
2 2167
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******* *******@TK2MSFT NGP15.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(inte rfaceParameters , BaseClass())(0) .AnyStringPrope rty.
works ok in the command window. Does not work at code.

I am using in the watch window:
DirectCast(inte rfaceParameters , 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(interface Parameters.leng th-1) as baseclass
interfaceParame ters.CopyTo(bas es,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
1496
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
11202
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
1564
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 produced by Regex. The Collection's entries are all valid strings. If I CopyTo to a string array it compiles ok, but fails at run time with an InvalidCastException. It works if I CopyTo an object array. ... does this mean that the MatchCollection's...
6
1398
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: ->this.backend = (BackEnd)System.Web.HttpContext.Current.Session<-- in another project of the same solution I got: public CustomerAccess(BackEnd backend, string culture) { this.backend = (BackEnd)System.Web.HttpContext.Current.Session; ....
1
3769
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 variant which appears to VB.NET as a System.Object. I want to convert this to a System.Array so that I can iterate over its elements. The VB6 usage is described at www.turbocontrol.com/TechTips/20020426.htm. If I try something similar in...
6
1900
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 pointer/array 'ends'". why would this happen? Is it a bug of VC8?
1
2052
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
3462
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 are tied.) The query runs fine (compiles and runs without throwing any errors). I can see in my "watch" in the debugger that the variable has the correct query text. But, when I open a new recordset with the query, I get no records. To be...
15
5210
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 execute the SQL code. Here's the corresponding snippets: 'declare variables: Dim strProc As String
0
8969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8788
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9263
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8210
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.