arraylist to array

G

Guest

I would simply like to convert the arraylist i read in to an array of type
double. The code below gives me an error that at least one element in the
source array could not be cast down to the destination array. What do I need
to change?

using System;
using System.IO;
using System.Collections;
namespace Program2
{
class Class1
{
static public void Main(string[] args)
{
{
StreamWriter swa=new StreamWriter("c:\\afinal.txt");
StreamReader sra=new StreamReader("c:\\Acolumn.txt");
string aLine="";

ArrayList aColumn = new ArrayList ();
while (aLine != null)
{
aLine=sra.ReadLine();
if (aLine != null)
aColumn.Add(aLine);
Console.WriteLine(aLine);
}
Console.ReadLine();
sra.Close();

double[] AField = (double[])aColumn.ToArray (typeof (double));
Console.WriteLine (AField);
}
Console.ReadLine();
}}}
 
J

Jon Skeet [C# MVP]

Christine said:
I would simply like to convert the arraylist i read in to an array of type
double. The code below gives me an error that at least one element in the
source array could not be cast down to the destination array. What do I need
to change?

You need to add doubles to the ArrayList instead of strings. Parse the
doubles as you go, and add the parsed double to the ArrayList.
 
M

Mattias Sjögren

What do I need to change?

You're currently putting strings into the ArrayList. So you can either
use ToArray() to retrieve a string[], or change the code to convert
the string to a double before putting it into the ArrayList (but keep
in mind that that would cause boxing). Personally I'd probably do
somehting like this

double[] AField = new double[aColumn.Count];
for ( int i = 0; i < AField.Length; i++ )
AField = Double.Parse((string)aColumn);



Mattias
 
G

Guest

I thought that was working in my code, but apparently not. I got an error
message that says Input string was not in a correct format.

Mattias Sjögren said:
What do I need to change?

You're currently putting strings into the ArrayList. So you can either
use ToArray() to retrieve a string[], or change the code to convert
the string to a double before putting it into the ArrayList (but keep
in mind that that would cause boxing). Personally I'd probably do
somehting like this

double[] AField = new double[aColumn.Count];
for ( int i = 0; i < AField.Length; i++ )
AField = Double.Parse((string)aColumn);



Mattias
 
J

Jon Skeet [C# MVP]

Christine said:
I thought that was working in my code, but apparently not. I got an error
message that says Input string was not in a correct format.

And what was the input string?
 
G

Guest

I'm sorry, that's not entirely correct. The input at that point of the code
is an arraylist. The arraylist came from a text file of numbers.
 
J

Jon Skeet [C# MVP]

Christine said:
The input string is just a text file of numbers.

Well it clearly failed with one of those input strings.

I suggest you catch the exception and print out what string it was that
caused it.
 
J

Jon Skeet [C# MVP]

Christine said:
I'm sorry, that's not entirely correct. The input at that point of the code
is an arraylist. The arraylist came from a text file of numbers.

But you're parsing each string in turn, according to your previously
posted code. One of those strings isn't being parsed. You need to find
out which, why it's there, and why it's not being parsed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top