473,465 Members | 1,917 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Illegal typecast exception?

Hello,

I have an array list whose elements are the following
types:

float
int
float
int
....etc.

I have the following code dealing with the elements:

float f0 = (float)list[0];
float f1 = (float)list[1];
float f2 = (float)list[2];
float f3 = (float)list[3];

Although the code compiles fine, I get a "Specified cast
is not valid." runtime exception on f1 and f3. I can fix
it by doing the following:

float f1 = (float)(int)list[1];
float f3 = (float)(int)list[3];

However, that's rediculous since in reality my code is a
loop that merely does something like:

for (i = 0; i < list.Count; ++i)
floatArray[i] = (float)list[i];

What's going on?

Thanks,
Ray Mitchell
Nov 15 '05 #1
3 3216
Ray Mitchell <Ra***************@MeanOldTeacher.com> wrote:
I have an array list whose elements are the following
types:

float
int
float
int
...etc.

I have the following code dealing with the elements:

float f0 = (float)list[0];
float f1 = (float)list[1];
float f2 = (float)list[2];
float f3 = (float)list[3];

Although the code compiles fine, I get a "Specified cast
is not valid." runtime exception on f1 and f3. I can fix
it by doing the following:

float f1 = (float)(int)list[1];
float f3 = (float)(int)list[3];

However, that's rediculous since in reality my code is a
loop that merely does something like:

for (i = 0; i < list.Count; ++i)
floatArray[i] = (float)list[i];

What's going on?


What's going on is that you're trying to unbox an int as a float, and
that won't work as you've already seen. What you can do is:

for (int i=0; i < list.Count; i++)
{
if (list[i] is float)
{
floatArray[i] = (float)list[i];
}
else if (list[i] is int)
{
floatArray[i] = (float)(int)list[i];
}
else
{
// Error handling
}
}

or you could use:

for (int i=0; i < list.Count; i++)}
{
floatArray[i] = Convert.ToSingle(list[i]);
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
Ray Mitchell wrote:
float f1 = (float)(int)list[1];
float f3 = (float)(int)list[3];


Since you'll be casting to them all to float in the end, why not cast
them to float as you add them to the array?

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
Nov 15 '05 #3
Hi,
You have boxed a value type. When unboxing the type the following rule is
applied

"For an unboxing conversion to a given value type to succeed at run time,
the value of the source argument must be a reference to an object that was
previously created by boxing a value of that value type. If the source
argument is null or a reference to an incompatible object, an
InvalidCastException is thrown."

According to the rule your unboxing "int" type to a "float" and they are
not the same, so the exception is thrown. For more details read read about
Boxing/Unboxing.

The following works for me...

for (i = 0; i < list.Count; ++i)
floatArray[i] = System.Convert.ToSingle(list[i]);

Hope that helps.

_Rakesh
--------------------
Content-Class: urn:content-classes:message
From: "Ray Mitchell" <Ra***************@MeanOldTeacher.com>
Sender: "Ray Mitchell" <Ra***************@MeanOldTeacher.com>
Subject: Illegal typecast exception?
Date: Tue, 23 Dec 2003 11:11:03 -0800
Lines: 35
Message-ID: <00****************************@phx.gbl>
MIME-Version: 1.0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
Thread-Index: AcPJiIIINxNAWqb9SV+CARJFr2p8Xg==
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
Newsgroups: microsoft.public.dotnet.languages.csharp
Path: cpmsftngxa07.phx.gbl
Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.languages.csharp:207532
NNTP-Posting-Host: tk2msftngxa14.phx.gbl 10.40.1.166
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

Hello,

I have an array list whose elements are the following
types:

float
int
float
int
...etc.

I have the following code dealing with the elements:

float f0 = (float)list[0];
float f1 = (float)list[1];
float f2 = (float)list[2];
float f3 = (float)list[3];

Although the code compiles fine, I get a "Specified cast
is not valid." runtime exception on f1 and f3. I can fix
it by doing the following:

float f1 = (float)(int)list[1];
float f3 = (float)(int)list[3];

However, that's rediculous since in reality my code is a
loop that merely does something like:

for (i = 0; i < list.Count; ++i)
floatArray[i] = (float)list[i];

What's going on?

Thanks,
Ray Mitchell

Rakesh, EFT.

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: rob | last post by:
Dear All, I am getting an error "Illegal cross-thread operation". I am not creating any threads. I know internally there are many threads running, though. Does that mean that no form can call a...
1
by: masood.iqbal | last post by:
I have a few questions regarding overloaded typecast operators and copy constructors that I would like an answer for. Thanks in advance. Masood (1) In some examples that I have seen...
12
by: [Yosi] | last post by:
What I should do to return back the permissin I had in VS2003 , that allowd me to access public methode of one Class from other Thread. I have Class A(FORM) which create new thread (new class B),...
0
by: Bruno van Dooren | last post by:
Hi, I am debugging a dll that is used in an application that calls all functions with C calling conventions, and cannot handle exceptions. Every 1-2 months there is a crash due to an unhandled...
12
by: Wilfried Mestdagh | last post by:
Hi, Using P/Invoke I use PostMessage to send a message from one thread to another. I'm pretty sure this has already worked in dotnet. but I've upgraded version 2 few day ago. Now I have an...
6
by: BlueTrin | last post by:
Hello I was adapting a C version of SolvOpt in C++ to use it within a virtual class. However I am stuck with the overriding of evaluation and gradiant functions. cStepCurveEvaluator.cpp...
16
by: DBC User | last post by:
Hi, I have a small XML file, I uploaded to a web page. I have the following code to convert the content I downloaded from web to xml and is giving "Illegal characters in path", but when I try to...
3
by: ryan.gilfether | last post by:
I have a problem that I have been fighting for a while and haven't found a good solution for. Forgive me, but my C++ is really rusty. I have a custom config file class: class ConfigFileValue...
1
by: crusson | last post by:
(edited to add): this is in Visual Basic .net I am at a complete loss... I've been building a program on my machine, running it out of the developer with the f5 key and builidng it and running...
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
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,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.