Reputation: 69
I have this code in C# Win Forms, which compares some numbers but it gives me following error:
InvalidCastException
Invalid cast from "Char" to "single".
I dont understand this error.. eny enlightment would be nice :)
the error come in this line
float old_list_diff = Convert.ToSingle(ReadLine[0]) - Convert.ToSingle(ReadLine[i+1]);
here is my full code.:
private void button7_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
StreamReader SR = new StreamReader(Application.StartupPath + @"\old_Score_list.txt");
string ReadLine = SR.ReadToEnd();
for (int i = 0; i < 14; i++)
{
float new_list_diff = Convert.ToSingle(Score_list.Items[0]) - Convert.ToSingle(Score_list.Items[i+1]);
float old_list_diff = Convert.ToSingle(ReadLine[0]) - Convert.ToSingle(ReadLine[i+1]);
//int old_list_diff = 20;
//skifter farve efter sidste i
if (new_list_diff > old_list_diff == true)
{
//listBox1.ForeColor = Color.Green;
listBox1.Items.Add("?");
}
else
{
//listBox1.ForeColor = Color.Red;
listBox1.Items.Add("?");
}
}
}
Upvotes: 0
Views: 320
Reputation: 3335
This conversion is not supported. Please take a look at the list of conversions: http://msdn.microsoft.com/en-us/library/system.convert.tosingle.aspx You may want to convert these characters to an integer (Int16 for example) value first.
Upvotes: 1
Reputation: 113272
string ReadLine = SR.ReadToEnd();
Gives you a big string of the whole file.
ReadLine[0]
Gives you the first character, so maybe just '0' or just '1'. That can't be cast to a float.
I'd guess you wanted:
string[] lines = SR.ReadLines();
Which would give you an array of strings, one for each line. Then lines[0]
would indeed be (if in the correct format) a string you could convert to a float, but I'm having to guess a bit about what you actually want.
Upvotes: 2
Reputation: 167
ReadLine is a string, and in C#, a string is really an array of characters. The way you've written your code, the program is reading the first line of the file, into the variable ReadLine. It seems like you're trying to treat ReadLine as an array of strings, when it is actually an array of characters.
So, if ReadLine = "Hello World!", then ReadLine[0] = "H"
I think you want to load the lines from the file into an Array of strings. Then you might be able to access the contents the way your code is written.
Upvotes: 2
Reputation: 87228
Well, you can't convert between a Char and a Single (a.k.a. float
). Do you really want to convert the first character of a string and the 2nd-15th characters of that string to a float
number? If that's really the case, you can try converting it first to an integer, then cast that integer to float. But notice that the conversion between Char and Int32 will return the ASCII (unicode) value of the character, which I'm not sure that's what you want...
Upvotes: 1
Reputation: 19469
You can get the InvalidCastException because the value is either a non-numeric type (like "ABCD") or exceeds the range for that type (number too large or too small).
If you attach the debugger and inspect the values in ReadLine[0] and ReadLine[i+1] you should see the value that it cannot cast.
Upvotes: 0