Hello again,

I hopefully have a quick and simple question. I'm working with 5 forms to that will be holding data over them and then have the ability to add, delete, edit the data for each person entered into the form (student wise).

So far I've managed to create 2 classes and my 5 forms. I've filled in my student.cs but having trouble with my main.cs (the one that will be taking full control of most of the forms).

student.cs:

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

namespace maint_student_scores
{
    class student
    {
        private string name;
        private List<int> Scores = new List<int>();

        public int scoreTotal()
        {
            int Total = 0;
            foreach (int score in Scores)
            {
                Total += score;
            }
            return Total;           
        }

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

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

        public void editScore(int Score, int index)
        {
            Scores.Remove(index);
            Scores.Insert(index, Score);
        }

        public void deleteScore(int index)
        {
           Scores.Remove(index);
            
        }


    }
}

main.cs:

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

namespace maint_student_scores
{
    class main
    {
        private List<student> Students = new List<student>();



        public Array allStudents()
        {




        }


        public int scoreTotal()
        {
           
        }


        public int scoreCount()
        {
          
        }


        public int average()
        {


        }
    }
}

I'm not sure if I did my main.cs correctly in the setup for the array. The way it works is that you enter a student name (or select one that is there), it will then show the Score Total, average, score count (how many scores in the the array) in a text box, I know how to do that, done that in the past and isn't too hard. But I'm confused on how I should be storing the array as a List<student> or should I be storing it someway else?

What is the return value of public Array allStudents() to be used for? I'm trying to figure out why you decided on Array as the return type.

What is the return value of public Array allStudents() to be used for? I'm trying to figure out why you decided on Array as the return type.

See I might have it wrong, but I was figuring that the Array allStudents() would put the list of students in my list box? Again I might be wrong, so correct me if I am.

See I might have it wrong, but I was figuring that the Array allStudents() would put the list of students in my list box? Again I might be wrong, so correct me if I am.

Not necessarily wrong. I was just curious. You could load the list box using the existing Students list.

Not necessarily wrong. I was just curious. You could load the list box using the existing Students list.

I guess yeah, but I think later in my project because I have to with 5 different forms, and allow selection of students and all their scores\data is stored under the array per their name it would be more functional to make the method Array. Because I have to enter the students -> be able to add\update\delete scores per student.

I'll play around with it a bit, because I'm sure I'm going to confuse myself some more later on.

I guess yeah, but I think later in my project because I have to with 5 different forms, and allow selection of students and all their scores\data is stored under the array per their name it would be more functional to make the method Array. Because I have to enter the students -> be able to add\update\delete scores per student.

I'll play around with it a bit, because I'm sure I'm going to confuse myself some more later on.

Nope, don't use Array because you just wind up boxing/unboxing the data. Stick to your strongly typed List<student> and just pass that into each form:

class student
        {
            private string name;
            public string Name // getter/setter property...
            {
                get { return name; }
                set { name = value; }
            }
            private List<int> Scores = new List<int>();

            // your other code here I left out for brevity...

            // completely optional...,but to demonstrate how to override ToString to return name
            public override string ToString()
            {
                return this.name;
            }
        }

        // your main form
        class MainForm : Form
        {
            List<student> Students = new List<student>();

            // Call one of my other forms....
            public void LoadNextForm()
            {
                MyOtherForm form2 = new MyOtherForm(Students); // pass the list to the form...
            }
        }

        // shows how your form can use an existing Students list....
        class MyOtherForm : Form
        {
            List<student> Students; // maintain a reference to same list for each form...
            ListView listView1 = new ListView();

            // pass the list into your constructor...
            public MyOtherForm(List<student> students)
            {
                this.Students = students; // set this form's reference to the list...
            }

            public void LoadListView()
            {
                foreach (student s in Students)
                {
                    // populate/add using ToString override...
                    listView1.Items.Add(s.ToString());

                    // or, using Property for student name...
                    //listView1.Items.Add(s.Name);
                }
            }
        }
// your main form
        class MainForm : Form
        {
            List<student> Students = new List<student>();
 
            // Call one of my other forms....
            public void LoadNextForm()
            {
                MyOtherForm form2 = new MyOtherForm(Students); // pass the list to the form...
            }
        }
 
        // shows how your form can use an existing Students list....
        class MyOtherForm : Form
        {
            List<student> Students; // maintain a reference to same list for each form...
            ListView listView1 = new ListView();
 
            // pass the list into your constructor...
            public MyOtherForm(List<student> students)
            {
                this.Students = students; // set this form's reference to the list...
            }
 
            public void LoadListView()
            {
                foreach (student s in Students)
                {
                    // populate/add using ToString override...
                    listView1.Items.Add(s.ToString());
 
                    // or, using Property for student name...
                    //listView1.Items.Add(s.Name);
                }
            }
        }

Just to be sure I'm doing this correctly, I would create 2 other class files that inhert. the functionality of my main.cs? or do I code everything into my student.cs?
Thank you.

// your main form
        class MainForm : Form
        {
            List<student> Students = new List<student>();
 
            // Call one of my other forms....
            public void LoadNextForm()
            {
                MyOtherForm form2 = new MyOtherForm(Students); // pass the list to the form...
            }
        }
 
        // shows how your form can use an existing Students list....
        class MyOtherForm : Form
        {
            List<student> Students; // maintain a reference to same list for each form...
            ListView listView1 = new ListView();
 
            // pass the list into your constructor...
            public MyOtherForm(List<student> students)
            {
                this.Students = students; // set this form's reference to the list...
            }
 
            public void LoadListView()
            {
                foreach (student s in Students)
                {
                    // populate/add using ToString override...
                    listView1.Items.Add(s.ToString());
 
                    // or, using Property for student name...
                    //listView1.Items.Add(s.Name);
                }
            }
        }

Just to be sure I'm doing this correctly, I would create 2 other class files that inhert. the functionality of my main.cs? or do I code everything into my student.cs?
Thank you.

Well, just to be sure, you plan to create a separate class file for each form I imagine, using the designer too, right?

As far as main.cs is concerned, I would eliminate that altogether and put the declaration/instantiation of Students list in the whatever your main Form is.

Since you are storing all scores for a student with that student List entry, there is no need to have an additional wrapper class to hold additional details about an individual student.

Even if you want to average all students' scores, you could put this functionality in the student class. For example public static int averageAllStudents(List<student> Students) :

class student
        {
            private string name;
            public string Name // getter/setter property...
            {
                get { return name; }
                set { name = value; }
            }
            private List<int> Scores = new List<int>();

            // your other code here I left out for brevity...

            // average all test scores for class/school or whatever is in our list...
            public static int averageAllStudents(List<student> Students)
            {
                int totScores = 0; // operand is total of all scores for all students
                int totCount = 0;  // dividend is total count of all score entries for all students

                // for every student
                foreach (student s in Students)
                {
                    // and for every student score
                    foreach (int score in s.Scores)
                    {
                        totScores += score; // add score
                        totCount++; // increment count
                    }
                }
                return totScores / totCount; // return average
            }

            // completely optional...,but to demonstrate how to override ToString to return name
            public override string ToString()
            {
                return this.name;
            }

        }

Well, just to be sure, you plan to create a separate class file for each form I imagine, using the designer too, right?

As far as main.cs is concerned, I would eliminate that altogether and put the declaration/instantiation of Students list in the whatever your main Form is.

Since you are storing all scores for a student with that student List entry, there is no need to have an additional wrapper class to hold additional details about an individual student.

Ok make sense, 3 of my forms require me to pull the data from the student array (I.E. show current scores) and the allow the ability to add\delete them. I guess I'm confused that if I put the array in the main form that the other forms won't be able to access it? Again I might just be thinking too far down the road.

Ok make sense, 3 of my forms require me to pull the data from the student array (I.E. show current scores) and the allow the ability to add\delete them. I guess I'm confused that if I put the array in the main form that the other forms won't be able to access it? Again I might just be thinking too far down the road.

No, now would be the time to think about that, but that problem is solved when you pass the list into the form as I demonstrated using class MyOtherForm : Form as an example.

No, now would be the time to think about that, but that problem is solved when you pass the list into the form as I demonstrated using class MyOtherForm : Form as an example.

Ah gotcha, and one last real quick one before I dive into the insanity of my programming ways lol, when your saying class myOtherForm : Form the myOtherForm = the name of the other form(s) I want to be able to access that class.cs and Form = the class name (I.E. student).

I'm sorry if I sound dumb with this, I just like to make sure my details are clearly put before I dive into it head first and then cause a bunch of issue and look even stupider, thanks.

Ah gotcha, and one last real quick one before I dive into the insanity of my programming ways lol, when your saying class myOtherForm : Form the myOtherForm = the name of the other form(s) I want to be able to access that class.cs and Form = the class name (I.E. student).

I'm sorry if I sound dumb with this, I just like to make sure my details are clearly put before I dive into it head first and then cause a bunch of issue and look even stupider, thanks.

Exactly! Here is an example....

// STUDENT CLASS
    public class student
    {
        // storage/getter/setter property...
        public string Name { get; set; }

        public List<int> Scores = new List<int>();
        

        /* ...
         * your other code here I left out for brevity...
         * ...
         * */

        // average all test scores for class/school or whatever is in our list...
        public static int averageAllStudents(List<student> Students)
        {
            int totScores = 0; // operand is total of all scores for all students
            int totCount = 0;  // dividend is total count of all score entries for all students

            // for every student
            foreach (student s in Students)
            {
                // and for every student score
                foreach (int score in s.Scores)
                {
                    totScores += score; // add score
                    totCount++; // increment count
                }
            }
            return totScores / totCount; // return average
        }

        // completely optional...,but to demonstrate how to override ToString to return name
        public override string ToString()
        {
            return this.Name;
        }
    }

Then, for your main Form (Form1):

public partial class Form1 : Form
    {
        List<student> Students;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Students = new List<student>(); // create the list

            // add some items so we can see in the listview test on Form2
            student s1 = new student();
            s1.Name = "George Carnegie";
            s1.Scores.Add(100);
            s1.Scores.Add(95);
            Students.Add(s1);

            student s2 = new student();
            s2.Name = "Bill Butler";
            s2.Scores.Add(85);
            s2.Scores.Add(79);
            Students.Add(s2);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2(Students); // pass list to form...
            form2.ShowDialog();
        }
    }

Then, for another Form (Form2):

public partial class Form2 : Form
    {
        List<student> Students;

        public Form2(List<student> Students)
        {
            InitializeComponent();

            this.Students = Students;
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            // loading a ListView control with the data...
            foreach (student s in Students)
                listView1.Items.Add(new ListViewItem(s.ToString()/*or s.Name* works too*/));
        }
    }

Any changes you make to the List inside of Form2, created in Form1 and past into Form2 above, will also be returned to Form1 because it is the same List. This will be true for the List passed back and forth to all the forms.

Comments
Who said I was tenacious?? Great! :)

*High Five*, thanks for the example I get it completely now. You guys here are awesome!

Your welcome and happy coding! Please mark thread as SOLVED if your questions have been answered.

If you run into a problem somewhere, just create a new thread with that problem.

Cheers!

{
            // loading a ListView control with the data...
            foreach (student s in Students)
                listView1.Items.Add(new ListViewItem(s.ToString()/*or s.Name* works too*/));
        }

I get what that is doing, but where are you calling the listView1.Items.Add function from?

{
            // loading a ListView control with the data...
            foreach (student s in Students)
                listView1.Items.Add(new ListViewItem(s.ToString()/*or s.Name* works too*/));
        }

I get what that is doing, but where are you calling the listView1.Items.Add function from?

Not sure what you mean, because it was being called from the form's Load event until you removed it to ask this question. What are you asking about exactly?

Here is that section of code again:

public partial class Form2 : Form
    {
        List<student> Students;
 
        public Form2(List<student> Students)
        {
            InitializeComponent();
 
            this.Students = Students;
        }
 
        private void Form2_Load(object sender, EventArgs e)
        {
            // loading a ListView control with the data...
            foreach (student s in Students)
                listView1.Items.Add(new ListViewItem(s.ToString()/*or s.Name* works too*/));
        }
    }

Not sure what you mean, because it was being called from the form's Load event until you removed it to ask this question. What are you asking about exactly?

Here is that section of code again:

LOL... I feel super stupid, I figured what I did wrong, forgot to name my listboxes, HAHA... Go figure thats why I could't get stuff to work. Thanks again.

private void Form2_Load(object sender, EventArgs e)
        {
            // loading a ListView control with the data...
            foreach (student s in Students)
                listView1.Items.Add(new ListViewItem(s.ToString()/*or s.Name* works too*/));
        }

Should you have {} for the foreach ;).

Edited 7 Years Ago by EvilLinux: n/a

First Form:

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

namespace maint_student_scores
{
    public partial class maintain_student_scores : Form
    {
        List<student> Students;

        public maintain_student_scores()
        {
            InitializeComponent();
        }

        private void maintain_student_scores_Load(object sender, EventArgs e)
        {
            Students = new List<student>();  //create our student list

            // add some items so we can see in the listview test on Form2
            student s1 = new student();
            s1.name = "George Carnegie";
            s1.Scores.Add(100);
            s1.Scores.Add(95);
            Students.Add(s1);

            student s2 = new student();
            s2.name = "Bill Butler";
            s2.Scores.Add(85);
            s2.Scores.Add(79);
            Students.Add(s2);

            foreach (student s in Students)
            
            studentList.Items.Add(new ListViewItem(s.ToString()));
            

        }

        private void addBtn_Click(object sender, EventArgs e)
        {
            newstudent newStudentFrm = new newstudent(Students);
            newStudentFrm.ShowDialog();
        }

        private void updateBtn_Click(object sender, EventArgs e)
        {
            scoreupdate scoreupdateFrm = new scoreupdate(Students);
            scoreupdateFrm.ShowDialog();
        }

        private void deleteBtn_Click(object sender, EventArgs e)
        {
            
        }

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

        private void scoretotalTbox_TextChanged(object sender, EventArgs e)
        {

        }

        
    }
}

Second Form:

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

namespace maint_student_scores
{
    public partial class scoreupdate : Form
    {
        List<student> Students;

        public scoreupdate(List<student> Students)
        {
            InitializeComponent();

            this.Students = Students;
        }

        private void updatescores_Load(object sender, EventArgs e)
        {
            foreach (student s in Students)
                        
            scoreList.Items.Add(new ListViewItem(s.ToString()));
            
        }

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

        private void clearscoresBtn_Click(object sender, EventArgs e)
        {
           
        }

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

        private void addBtn_Click(object sender, EventArgs e)
        {
            updatescores_add updateScoresAddFrm = new updatescores_add();
            updateScoresAddFrm.ShowDialog();
        }

        private void updateBtn_Click(object sender, EventArgs e)
        {
            updatescores_update updateScoresUpdateFrm = new updatescores_update();
            updateScoresUpdateFrm.ShowDialog();
        }

        
    }
}

I tired your code (for the name\scores), but when I build and have it list in my listBox it doesn't show the name just:
ListViewItem: {maint_student_scores.student}
ListViewItem: {maint_student_scores.student}
same in my other form that should only pull up the scores of the student selected.

I'm getting really lost over all with this project.

Edited 7 Years Ago by EvilLinux: n/a

I've seen your other thread and also that Ryshad has pointed out the ToString override. You can stop posting changes to this thread now and we will focus on the new one--OK?

I've seen your other thread and also that Ryshad has pointed out the ToString override. You can stop posting changes to this thread now and we will focus on the new one--OK?

Yeah sorry, I meant to close this one but I figured my issue and couldn't remove it from here, so anything here is fixed.

k...firstly, when you use if/for/foreach/etc structures. You can ignore the {} if the code to be operated is a single line:

if (i==1)
      dosomething();

if you have more than one line of code to process within the structure then you need to encase them in {}.

Secondly, when you say anything here is fixed, you mean the code i gave you has solved the issue and everything is fine now, yes?

Remember to mark the thread as solved if your question has been answered :)

Edited 7 Years Ago by Geekitygeek: n/a

This question has already been answered. Start a new discussion instead.