string array or int array?

J

John Salerno

All this time I've been using a string array for my program, but I think
I might want to actually use the values (0s and 1s) as integers, so is
there an easy way to convert this? Should it happen when the file (of 0s
and 1s) is being read into the array, or should I read it as a string
array first, and then convert it to int? I tried one thing, but it
didn't work:

StreamReader readSwitchValues = new StreamReader(path);

string[] completeFile = readSwitchValues.ReadToEnd().Split();
int[] allSwitchValues = Int32.Parse(completeFile);
readSwitchValues.Close();
 
J

John Salerno

John said:
All this time I've been using a string array for my program, but I think
I might want to actually use the values (0s and 1s) as integers, so is
there an easy way to convert this? Should it happen when the file (of 0s
and 1s) is being read into the array, or should I read it as a string
array first, and then convert it to int? I tried one thing, but it
didn't work:

StreamReader readSwitchValues = new StreamReader(path);

string[] completeFile = readSwitchValues.ReadToEnd().Split();
int[] allSwitchValues = Int32.Parse(completeFile);
readSwitchValues.Close();

I tried this:

StreamReader readSwitchValues = new StreamReader(path);

string[] completeFile = readSwitchValues.ReadToEnd().Split();
readSwitchValues.Close();

int[] allSwitchValues = new int[640];
int i = 0;

foreach (string status in completeFile)
allSwitchValues[i++] = Int32.Parse(status);

but the Parse method is throwing an exception.

"Input string was not in a correct format."
 
T

Truong Hong Thi

I see in your snippet, you parse an array of string to get an array of
int. That is not possible. If you want that, try something like
following:

int[] ParseIntValues(string[] stringValues)
{
int theInts = new int[stringValues.Length];
foreach(int i = 0, n = stringValues.Length, i < n; i++)
{
theInts = Int32.Parse(stringValues, NumberStyle.Integer,
CultureInfo.InvariantCulture);
}

return theInts;
}

I see you made some similar posts about this. If you describe what you
are trying to do instead, we could discuss the solution.
 

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