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("?");
}
}
}
Gives you a big string of the whole file.
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:
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.