I have:
private void btnAddScore_Click(object sender, EventArgs e)
{
if (IsInt32())
{
txtScores.Text += txtScore.Text + " ";
txtScore.Text = "";
}
}
and:
private void button2_Click(object sender, EventArgs e)
{
if (IsValidData())
{
List<string> result = txtScores.Text.Split(' ').ToList();
student = new Student(txtName.Text, result.Select(int.Parse).ToList());
this.Close();
}
}
I'm trying to use my btnAddScore to build a string of scores from my txtScore to my txtScores. This I believe I'm doing correctly. Then I'm converting that string into a list by Parsing each element with " ". I then further convert the List < string > into a List < int >. No compiler errors but a runtime error of "FormatException was undandled" and points to (int.Parse). I've read that int.Parse will cause this if used on an empty string, but I don't see how that's the case if it is.
Because you are appending a white space the "Split" method returns an empty element at the end wich you are not expecting, add na option "SplitOptions.RemoveEmptyEntries" (from head, check if it is the correct name) and your code will work.