Hi, I am working on an assignment in c# where I have to maintain student scores...there is a few questions up here already about it but I am still having problems. I need to create a program that allows the user to enter student names and scores...

I gather I need to use a list to store these values but I am having problems understanding how to go about this.. I just want to be able add student names and scores to a list and pass them back to the main form..

I'll post the code i have and project specs below..
any guidence would really be appreciated...really struggling with c#.
thanks..

Student class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


{
   public class Student
    {
       public string name { get; set; }
       public List<int> Scores = new List<int>();

       public override string ToString()
       {
           return name;
       }
    }

}

Add Student Form

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


{
    public partial class AddStudent : Form
    {
        public AddStudent()
        {
            InitializeComponent();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

Main Form :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


{
    public partial class Main : Form
    {
        List<Student> Students;
        public Main()
        {
            InitializeComponent();
        }

        private void Main_Load(object sender, EventArgs e)
        {
            Students = new List<Student>();

            Student s1 = new Student();
            s1.name = "Joel Murach";
            s1.Scores.Add(100);
            s1.Scores.Add(100);
            Students.Add(s1);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form studentForm = new AddStudent();
            studentForm.ShowDialog();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form updateForm = new updateStudent();
            updateForm.ShowDialog();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

Project specs:

The design of the Student Scores form

Operation
To display the total, count, and average for a student, the user selects the student from the list box. If the list box is empty, the total, count, and average labels should be cleared.
To add a new student, the user clicks the Add button to display the Add New Student dialog box.
To update an existing student’s scores, the user selects the student in the list box and clicks the Update button to display the Update Student Scores dialog box.
To delete a student, the user selects the student in the list box and clicks the Delete button.
The design of the Add New Student form

Operation
To add a new student, the user enters a student name and, optionally, one or more scores and clicks the OK button.
To add a score, the user enters a score and clicks the Add Score button. The score is added to the list of scores in the Scores label.
To remove all scores from the Scores label, the user clicks the Clear Scores button.
To cancel the add operation, the user clicks the Cancel button.
The design of the Update Student Scores
and Add/Update Score forms
Operation
To add a score, the user clicks the Add button and enters the score in the Add Score dialog box that’s displayed.
To update a score, the user selects the score, clicks the Update button, and changes the score in the Update Score dialog box that’s displayed.
To remove a score from the Scores list box, the user selects the score and clicks the Remove button.
To remove all scores from the Scores list box, the user clicks the Clear Scores button.
To accept all changes, the user clicks the OK button.
To cancel the update operation, the user clicks the Cancel button.
Specifications
This application should make sure that the user enters a name for a new student. However, a new student can be added without any scores.
This application should check all scores entered by the user to make sure that each score is a valid integer from 0 to 100.
When the application starts, it should load the list box with three sample students.
Hint
To get the labels that display the area and perimeter to look like the ones shown above, you’ll need to set the AutoSize property of the labels to False and the BorderStyle property to Fixed3D.S

Ahh Lists, one of the reasons I love C# so much.

So it looks like you are trying to retrieve the List of scores in your Student object right?

public class Student
{
    public Student()
    {
        this.Scores = new List<int>();
    }

    public Student(string Name, List<int> Scores)
    {
        this.Name = Name;
        this.Scores = Scores;
    }


    public string Name
    {
        get;
        set;
    }

    public List<int> Scores
    {
        get;
        set;
    }
}

So the example I posted is pretty much a mimic of your Student's object. Notice however, that it we don't pass in any parameters, I initialize the List.

As for accessing the data, using your List<Student> Students as an example, to retrieve a name you'd use Students[i].Name which returns a string (you can also set it, since it is a get/set). And as for the Scores, you are use Students[i].Scores[j]

When you access the Scores variable, it's like you are accessing the List. So you could get/set the whole list, or an index from it.

Now if you are a little puzzled about Lists, just look at them like Arrays. They are similar in concept, except Lists have a much larger size that has already been pre-set (I forgot the max size of a List), while an array you need to declare the size initally. A List is like an array when it comes to indexing and holding values, but like a Queue or Stack in that the size expands on its own.

Hopefully this answers your question

Thanks for the response...I've been battling with this for so long.

so I have used an example in the listbox which is joel murach and given it scores of 100 and it should display "Joel Murach 100| 100" in the listbox but atm I can only get it to display Joel Murach and not the scores..is this were i use the "Students[i].Scores[j]" you suggested above??

Sorry very confused on how i display both the name and score of the student..

Thanks

With the code you gave me abve my code is now..

Student class

public class Student
    {

       public Student()
       {
           this.Scores = new List<int>();
       }

       public Student(string Name, List<int> Scores)
       {
           this.Name = Name;
           this.Scores = Scores;
       }

       public string Name
       {
           get;
           set;
       }

       public List<int> Scores
       {
           get;
           set;
       }


       public override string ToString()
       {
           return Name;
       }


       public int scoreTotal()
       {
           int sum = 0;

           foreach (int score in Scores)
           {
               sum += score;
           }
           return sum;
       }

    }

}

Main Form:

 public partial class Main : Form
    {

        List<Student> Students;

        public Main()
        {
            InitializeComponent();


        }



        private void Main_Load(object sender, EventArgs e)
        {


            Students = new List<Student>();

            Student s1 = new Student();
            s1.Name = "Joel Murach";

            s1.Scores.Add(100);
            s1.Scores.Add(100);
            Students.Add(s1);


            listBoxStudents.Items.Add(s1);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Form studentForm = new AddStudent();
            studentForm.ShowDialog();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form updateForm = new updateStudent();
            updateForm.ShowDialog();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            listBoxStudents.Items.Remove(listBoxStudents.SelectedItems);
        }

        private void listBoxStudents_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}

Well sort of.

My suggestion, however, would be to first rebuild that Studnet object of yours. You don't need that "ToString()" function as you can retrieve the Name value with your get/set.

(Also, remember that when I say 'i' and 'j' I am refering to variables used for indexes).

Another note, that Student object, think of it like an object that holds a bunch of variables that work the same as usual. So while Student is custom, Name is simply a string within Student, and so on.

So just remember, when it comes to navigating a List, it's much like navigating an array.

May I ask, how do you want to display the items? Are you trying to store multiple scores per name?

I'm not much better with arrays haha

I'm quite confused right now..

multiple scores

it should display like this..

Well you could do something like this

Students[i] + " - " + String.Join(" | ", Students[i].Scores.ToArray())

That returns a string (the String.Join is a cool feature that joins all the values of an Array into a string, using the first parameter as the splitter between each value)

Also, think of an array like this. I assume you know Microsoft Excel? Well imagine the one column as an array. You retrieve values by the index (starting at 0).

So in excel I place a value in the first and second row of the same column. Now I want to retrieve a that send value well I do the ARRAYNAME[1]

And I get back the value.

Another way, think of it as a bunch of variables all stored in one nice item. So a bunch of ints for instance, all stored in a 'box', and then I can pick what I want from the box, or put into the box

Sorry for the slow reply.

I can't use arrays it has to be done using a list...
I still can't figure out how to get my scores to add...

Appreciate any help... (dumb it down haha)

Thanks

(Oh trust me, I encourage using Lists. The example I gave was because they are similar in concept).

You are adding the scored properly to the list. To add items to a list you simply use "Add(value)".

By adding the scores, do you mean like adding up all the values?

Ok so i made some progress...
We have to show on load some sample data..so student names and the scores they contain...
I have that working now...

I need to then be able to enter my own students name and scores in a dialog box and these then add back to the listbox where the sample hardcoded students already are?

problem 2: I need to get the score total, score count and average for the student selected in the list box and pass these values to the labels on the form.

my code at present is:

Student Class

public class Student
    {

        public Student()
        {
            this.Scores = new List<int>();
        }

        public Student(string Name, List<int> Scores)
        {
            this.Name = Name;
            this.Scores = Scores;
        }

        public string Name
        {
            get;
            set;
        }

        public List<int> Scores
        {
            get;
            set;
        }

        public override string ToString()
        {
            string names = this.Name;

            foreach(int myScore in Scores)
            {
                names += "|" + myScore.ToString();
            }

            return names;

        }

        public int GetscoreTotal()
        {
            int sum = 0;

            foreach (int score in Scores)
            {
                sum += score;
            }
            return sum;

        }
            public int GetScoreCount()
        {
            return Scores.Count;
        }


       public void addScore(int Score)
       {
           Scores.Add(Score);
       }

    }

}

Main form:

public partial class Main : Form
    {
        public List<Student> studentList = new List<Student>();
        List<Student> Students;

        public Main()
        {
            InitializeComponent();

        }

        private void Main_Load(object sender, EventArgs e)
        {

            // Entering Data to show on load.
            Students = new List<Student>();

            Student s1 = new Student();
            s1.Name = "Joel Murach";
            s1.Scores.Add(97);
            s1.Scores.Add(71);
            s1.Scores.Add(83);
            Students.Add(s1);    
            listBoxStudents.Items.Add(s1);

            Student s2 = new Student();
            s2.Name = "Doug Lowe";
            s2.Scores.Add(99);
            s2.Scores.Add(93);
            s2.Scores.Add(97);
            Students.Add(s2);
            listBoxStudents.Items.Add(s2);

            Student s3 = new Student();
            s3.Name = "Anne Boehm";
            s3.Scores.Add(100);
            s3.Scores.Add(100);
            s3.Scores.Add(100);
            Students.Add(s3);
            listBoxStudents.Items.Add(s3);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form studentForm = new AddStudent();
            studentForm.ShowDialog();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form updateForm = new updateStudent();
            updateForm.ShowDialog();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            while (listBoxStudents.SelectedItems.Count > 0)
            {
                listBoxStudents.Items.Remove(listBoxStudents.SelectedItems[0]);
            }
        }

        private void listBoxStudents_SelectedIndexChanged(object sender, EventArgs e)
        {


        } 

        private void txtScoreTotal_TextChanged(object sender, EventArgs e)
        {


        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

Add new student form:

public partial class AddStudent : Form
    {

        List<Student> Students;
        public AddStudent()
        {
            InitializeComponent();

        }

        public AddStudent(List<Student> Students)
        {
            this.Students = Students;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            lblScores.Text = String.Empty;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (txtScore.Text == string.Empty)
            {
                txtScore.Focus();
                return;
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            Students = new List<Student>();
            if (txtName.Text == string.Empty)
            {
                MessageBox.Show("Error, you must enter a name to continue");
            }



        }

        private void AddStudent_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {

        }
    }
}

You are looking pretty good.

So what are you stumpted by now? It looks like you are getting to your goal. Are you now trying to make your own form? That does the work the Load event does?

Okay I got an idea
- When adding a new record to the List, use the "Add(object)" function
- When trying to retrieve records use the index like "Students[0]"

(THere are also other abilities to like remove and insert).

Also, let me ask you, what's your knowledge of Arrays, List, Queues, and Stacks?

What I have working at the moment is students data coming up when the form loads that i have coded, i now need to click on the add student button, enter a name and scores for that student and click ok, clicking ok should then add this student to the list below the other students i have hard coded in...

the screenshot below shows the main form, that has the students hard coded in then the second form in the form that loads when the add new button is clicked i dont know how to add students there that then pass back to the list box?

My knowledge isn't that great if I am honest, I have missed quite alot of c# due to illness and hospital visits, I have tried to self teach from the book but just not getting anywhere...if you would be able to talk me through the code for something believe me I would greatly appreciate it!

For this type of application it would probably better suit you to use something like an XML file or database. The problem is passing data from one form to another. You're initiating the new(StudentList) in both forms which means that they would hold different data and no way of knowing what the other one contains. So a simple way would be Declaring your student list static in your main form public static List<Student> Students; And then when calling the add form declare it as follows List<Student> StudentListFromFirstForm = Form1.Students;

Yeah database would have made more sense but not allowed.
ah yes that does make more sense.

Im still confused on how I then add them to the list box from the dialog box though?
As in how would i pass the name in the textbox and the scores in the label to the listbox..

You can add the records, just like you did on the load. Pass it a collected name, and the scores

(I should also point out, remember that get/set we did for the object, you can use those on forms as well. So the form has the variable to work with, but you can access those values since you declared the form before showing it).

Thanks, I managed to work it out during the week!

Thanks for you help anyway :)

Awesome, that's great to hear. Sorry I didn't get back to you sooner (life got really busy for me).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.