Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

ArrayList ToArray() throwing exception (concatenate double[])

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Michael Howes - 23 Oct 2007 21:07 GMT
I have many double[] that each have a few thousand numbers in them.
 I need to concatenate groups of these double arrays into a new set of
double[].
 I don't know the total # of points.

 I thought it would be fairly easy to add these different double arrays
to different ArrayLists and then use ToArray to get all the numbers back
in one array. Sort of a cheap concatenate so I don't need to loop
through all the points or keep newing larger arrays

 when I call ToArray
 (double[])yData.ToArray(typeof(double))

 I get an exception; "At least one element in the source array could
not  be cast down to the destination array type."

 Umm..they were all valid doubles in arrays of doubles before being
added to the ArrayList

 any ideas what this might be?

thanks
 mike
Jon Skeet [C# MVP] - 23 Oct 2007 21:27 GMT
>   I have many double[] that each have a few thousand numbers in them.
>   I need to concatenate groups of these double arrays into a new set of
[quoted text clipped - 16 lines]
>
>   any ideas what this might be?

Did you add the *arrays* to the ArrayList, or each double in turn? If
you're adding the arrays, then ArrayList isn't going to do any
flattening for you, unless you call AddRange instead of Add.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Ignacio Machin ( .NET/ C# MVP ) - 23 Oct 2007 21:32 GMT
Hi,

Well, the message says everything, at least one element could not be
converted to double.

Do manually and see if you find any problem:

foreach( object o in GetElements )
double d = (double) o;

and see if you get an error and where it's located
Signature

Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.

>
>  I have many double[] that each have a few thousand numbers in them.
[quoted text clipped - 20 lines]
> thanks
>  mike
Göran Andersson - 23 Oct 2007 21:36 GMT
>  I have many double[] that each have a few thousand numbers in them.
>  I need to concatenate groups of these double arrays into a new set of
> double[].
>  I don't know the total # of points.

Of course you do. Each array has a length, you only have to add them.

>  I thought it would be fairly easy to add these different double arrays
> to different ArrayLists and then use ToArray to get all the numbers back
> in one array. Sort of a cheap concatenate so I don't need to loop
> through all the points or keep newing larger arrays

Any method that could do what you are trying to accomplish, would do
exactly what you are trying to avoid doing yourself.

>  when I call ToArray
>  (double[])yData.ToArray(typeof(double))
[quoted text clipped - 4 lines]
>  Umm..they were all valid doubles in arrays of doubles before being
> added to the ArrayList

You can't convert a double array into a double. The ToArray method will
produce an array with the same number of items as the ArrayList has, it
won't expand any arrays in the ArrayList.

Just create an array with the length of the sum of all the arrays that
you want to put in it, then use the CopyTo method to copy the data from
each array into the new array.

Signature

Göran Andersson
_____
http://www.guffa.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.