I have troubles trying to display a list of items/objects in a array.
What I'm doing is I'm adding students with a name, surname, age, and marks for subjects (Properties) to an array. If I added 4 students in this manner to the list I want to display all the students with their properties again in the richTextBox when the Display button is clicked.
How do I go about it in the "displayAll_Click" function?? Must I use a for loop of some sort?? Any help will be appreciated.
I have the following code:
In this fucntion I'm adding the student into the array:
private void btnAdd_Click(object sender, EventArgs e)
{
// Create new student and assign name etc provided by user
Student _Student = new Student();
_Student.Name = txtName.Text;
_Student.Surname = txtSurname.Text;
_Student.Age = Convert.ToInt32(txtAge.Text);
_Student.ITMark = Convert.ToInt32(txtIT.Text);
_Student.MathMark = Convert.ToInt32(txtMath.Text);
_Student.EngMark = Convert.ToInt32(txtEng.Text);
MessageBox.Show("Student added.");
// Increase counter and display how many students added so far
CountStudents++;
Average = Convert.ToInt32(txtIT.Text) + Convert.ToInt32(txtMath.Text) + Convert.ToInt32(txtEng.Text);
_Student.AverageMark = Average / 3;
//Add the newly added student to the ClassList array
ClassList[CountStudents - 1] = _Student;
//Clear the list after student is added
txtAge.Clear();
txtName.Clear();
txtSurname.Clear();
txtIT.Value = 0;
txtMath.Value = 0;
txtEng.Value = 0;
txtName.Focus();
}
And in this function I want to display it that was added in the array or list?
private void displayAll_Click(object sender, EventArgs e)
{
List<Student> _StudentList = new List<Student>();
Student _Student = new Student();
_Student.Name = txtName.Text;
_Student.Surname = txtSurname.Text;
_Student1.Age = Convert.ToInt32(txtAge.Text);
_Student.ITMark = Convert.ToInt32(txtIT.Text);
_Student.MathMark = Convert.ToInt32(txtMath.Text);
_Student.EngMark = Convert.ToInt32(txtEng.Text);
for (int i = 0; i < _StudentList; i++)
{
richTextBox1.Text = ("Student: " + Convert.ToString(CountStudents) +
"\nName: " + _Student.Name +
"\nSurname: " + _Student.Surname +
"\nAge: " + _Student.Age +
"\nStudent Average: " + Convert.ToString(_Student.AverageMark));
i++;
}
//Displaying all the students
MessageBox.Show("Display all students.");
}