472,960 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

ArrayTypeMismatchException

Hello!

This simple code:

decimal[] a = new decimal[] { 23.45m, 120.0m };
double[] b = new double[2];

a.CopyTo(b, 0);

throws an ArrayTypeMismatchException. I can't find anything in the docs
which suggests that this is expected behaviour. Can anybody please give me
an explanation for it?

--
Thank you,

Christopher Ireland

"There is no way to peace; peace is the way."
A. J. Muste

Sep 12 '08 #1
6 1977
http://msdn.microsoft.com/en-us/library/06x742cw.aspx

"ArrayTypeMismatchException
The type of the source Array cannot be cast automatically to the type of
the destination array."

In this case, there is no automatic conversion between decimal and
double - they are very different data types. Suggest you just loop? Or use:

double[] b = Array.ConvertAll(a, x =(double)x);

Marc
Sep 12 '08 #2
double[] b = Array.ConvertAll(a, x =(double)x);

btw, in C# 2.0 this is:

double[] b = Array.ConvertAll<decimal,double>(a, delegate (decimal x)
{return (double)x;});

Gotta love lambdas and the improved generic type inference ;-p

Marc
Sep 12 '08 #3
Marc,

Thanks for your reply!
In this case, there is no automatic conversion between decimal and
double - they are very different data types.
This is the point that interests me most.

I've read that automatic conversions are widening conversions. In this link
http://msdn.microsoft.com/en-us/library/08h86h00.aspx , Decimal->Double
conversions are stated as being widening conversions although with
occasional information loss. If Decimal->Double conversions are widening
conversions, they are therefore automatic, no? Do you have any information
which could clear this up for me please?

--
Thank you,

Christopher Ireland

Sep 12 '08 #4
Marc,
double[] b = Array.ConvertAll<decimal,double>(a, delegate (decimal x)
{return (double)x;});

Gotta love lambdas and the improved generic type inference ;-p
Love 'em, and would even settle for anonymous methods, but the components we
write have to target .net 1.1 and the code is full enough of #ifs as it is
<g>!

--
Thank you,

Christopher Ireland

Sep 12 '08 #5
Well, the precisions and scales don't tie up neatly - and simply not all
decimal values can be represented accurately as double, so there is no
implicit conversion; you can check this with just:

decimal x = 23.45M;
double y = x; // error - needs explicit cast

The table on that MSDN page tells you that a conversion is possible: it
doesn't claim it is implicit. There is rarely an implicit conversion if
there is a risk of data loss.

Marc
Sep 12 '08 #6
Marc,
The table on that MSDN page tells you that a conversion is possible:
it doesn't claim it is implicit. There is rarely an implicit
conversion if there is a risk of data loss.
So am I to understand that the terms "implicit conversion" and "automatic
conversion" are synonymous? If so, then you have helped me satisfy my
curiosity, thank you!

--
Thank you,

Christopher Ireland

Sep 12 '08 #7

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

Similar topics

14
by: Erik Frey | last post by:
Hello, Say I have the following two classes: class Base { } class Inherited : Base {
4
by: garak | last post by:
Hi, I got the following problem : I have an defined an Array of different Actions: public PRootActions AllActions = new PRootActions ; I got a dynamic method where all Actions or other...
2
by: Frank Pleyer via .NET 247 | last post by:
Hi, I got the following problem : I have an defined an Array of different Actions: public PRootActions AllActions = new PRootActions ; I got a dynamic method where all Actions or other...
6
by: Vitaly Zayko | last post by:
It's probably simple but I can't find a way how to copy number of bytes from one byte array to another? Just like Array.Copy(SourceArray, SourceIndex, DestArray, DestIndex, Length) does but in my...
4
by: yannis.corre | last post by:
Hello, I don't understand the difference of behavior of these two webservers : I'm uploading a file and I launch a thread that makes some treatments with the Request.Files. I saw that a new...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.