have 2 lists one for JuniorStudents and one for SeniorStudents. In the JuniorStudents I have a method for promoting a juniorstudent to a seniorStudent. I am first deleting the juniorstudent and then putting it into the seniorstudent list. However when I try to view the student when calling the method viewStudents I am not getting the student. Any ideas why this is so. Here is the code:

public class SeniorStudentsClass
    {
        public List<Student> studlist = new List<Student>();

public void ViewStudents()
        {
            for (int i = 0; i < studlist.Count; i++)
            {               
                Console.Write(studlist[i].Id + "\t");
                Console.Write(studlist[i].Year + "\t");
                Console.Write(studlist[i].Name + "\t");
                Console.Write(studlist[i].Surname + "\t");
                Console.Write(studlist[i].DOB + "\t");
                Console.Write(studlist[i].Addr);
                Console.WriteLine();
            }

  public class JuniorStudentsClass
    {
       public List<Student> mystudent = new List<Student>();  
       SeniorStudentsClass sc = new SeniorStudentsClass();

        public void PromoteStudents(int index)
        {
            SearchItem(index);
            Console.WriteLine("current record:");
            Console.WriteLine("id is:" + mystudent[index].Id);
            Console.WriteLine("year is:" + mystudent[index].Year);
            Console.WriteLine("name is:" + mystudent[index].Name);
            Console.WriteLine("surname is:" + mystudent[index].Surname);
            Console.WriteLine("dob is:" + mystudent[index].DOB);
            Console.WriteLine("address is:" + mystudent[index].Addr);
            Console.WriteLine("year is:"+mystudent[index].Year);
            if (((mystudent[index].Year== 7)) || ((mystudent[index].Year == 8)))
            {
                var student = mystudent[index];
                mystudent.RemoveAt(index);
                sc.studlist.Add(student);
                Console.WriteLine("student promoted to senior student");

        }

Recommended Answers

All 4 Replies

Your code is structured very bad. Where is Main? At least I (perhaps some more clever folks around here?) cannot figure out what you really want, with the code you gave.

I second ddanbe's comment. I looked at your code ... umm does this code even run?

No offense but you are declaring a SeniorStudentsClass inside itself. Maybe you should rebuild this code. Start with a main.

If you want a print class make it like

public class SeniorStudentsClass
{
    public static void ViewStudents(List<Student> studlist)
    {
        for (int i = 0; i < studlist.Count; i++)
        {
            Console.Write(studlist[i].Id + "\t");
            Console.Write(studlist[i].Year + "\t");
            Console.Write(studlist[i].Name + "\t");
            Console.Write(studlist[i].Surname + "\t");
            Console.Write(studlist[i].DOB + "\t");
            Console.Write(studlist[i].Addr);
            Console.WriteLine();
        }
    }
}

There now you have a class that will display any List<Student> you want depending on which one you pass in (you could also reference it in if you cared).

Then you would call it like this

//assume you have precreated the JuniorStudents List<Student>

SeniorStudentsClass.ViewStudents(JuniorStudents);

Here's just an example of how you might want to handle the print method. Your code has me all confused sorry to say, and as ddanbe has pointed out the structuring is pretty bad.

I would suggest that it looks like you're modifying a new student list, then printing out the old one. Make the junior class list and the senior class list global. Try using the global lists in promotestudents instead of making local ones. Also if you promote the student to senior class, before you delete him/her from junior class, you don't need that extra var student variable

Line 21 you create list of senior students in the junior students class. You add the students to this list when you promote them. This does not do anything to the list contained in the senior students class. How could it?

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.