Illegal typecast exception?

  • Thread starter Thread starter Ray Mitchell
  • Start date Start date
R

Ray Mitchell

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 = (float)list;

What's going on?

Thanks,
Ray Mitchell
 
Ray Mitchell said:
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 = (float)list;

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 is float)
{
floatArray = (float)list;
}
else if (list is int)
{
floatArray = (float)(int)list;
}
else
{
// Error handling
}
}

or you could use:

for (int i=0; i < list.Count; i++)}
{
floatArray = Convert.ToSingle(list);
}
 
Ray said:
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)
 
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 = System.Convert.ToSingle(list);

Hope that helps.

_Rakesh


--------------------
Content-Class: urn:content-classes:message
From: "Ray Mitchell" <[email protected]>
Sender: "Ray Mitchell" <[email protected]>
Subject: Illegal typecast exception?
Date: Tue, 23 Dec 2003 11:11:03 -0800
Lines: 35
Message-ID: <[email protected]>
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 = (float)list;

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
 
Back
Top