Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Adding to a list from another class

$
0
0
I have a program which contains JuniorStudents and SeniorStudents. I've made seperate classes and in each class there is a list. Now when the student is year 6 or year 7 I shall promote it to senior student. I am doing this in the PromoteStudent method but when I call the ViewStudent method the SeniorStudent class I'm not getting the item. Can someone help me please? Here is the code:
public class JuniorStudentsClass
    {
   public List<Student> mystudent = new List<Student>();
ublic void Addstudent()
        {
            Console.Write("enter students id:");
            s.Id = Int32.Parse(Console.ReadLine());
            Console.Write("enter students year:");
            s.Year = Int32.Parse(Console.ReadLine());
            Console.Write("enter name:");
            s.Name = Console.ReadLine();
            Console.Write("enter surname:");
            s.Surname = Console.ReadLine();
            Console.Write("enter dob:");
            s.DOB = Console.ReadLine();
            Console.Write("enter address:");
            s.Addr = Console.ReadLine();
            mystudent.Add(new Student(s.Id, s.Year, s.Name, s.Surname, s.DOB, s.Addr));

        }
 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("enter new year of student to promote");
            s.Year = Int16.Parse(Console.ReadLine());
            if (((s.Year == 7)) || ((s.Year == 8)))
            {               
                sc.List.Add(new Student(mystudent[index].Id, s.Year, mystudent[index].Name, mystudent[index].Surname, mystudent[index].DOB, mystudent[index].Addr));
                Console.WriteLine("student promoted to senior student");
            }
            else
            {
                mystudent.Add(new Student(mystudent[index].Id, s.Year, mystudent[index].Name, mystudent[index].Surname, mystudent[index].DOB, mystudent[index].Addr));
                Console.WriteLine("student promoted to next year");
            }
        }


        public int SearchItem(int search)
        {
            int found = -1;
            if (mystudent != null)
            {
                foreach (Student st in mystudent)
                {
                    found++;
                    if (Student.Equals(search, st))
                    {
                        break;
                    }

                }
            }
            return found;
        }

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

        public List<Student> List {
            get { return studlist; }
            set { studlist = value; }
        }

        public void Addstudent()
        {
            Console.Write("enter students id:");
            s.Id = Int32.Parse(Console.ReadLine());
            Console.Write("enter students year:");
            s.Year = Int32.Parse(Console.ReadLine());
            Console.Write("enter name:");
            s.Name = Console.ReadLine();
            Console.Write("enter surname:");
            s.Surname = Console.ReadLine();
            Console.Write("enter dob:");
            s.DOB = Console.ReadLine();
            Console.Write("enter address:");
            s.Addr = Console.ReadLine();
            studlist.Add(new Student(s.Id, s.Year, s.Name, s.Surname, s.DOB, s.Addr));   

        }  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 Student
    {
        Marks mrk = new Marks();
        private int id;
        private string name;
        private string surname;
        private string dob;
        private string address;
        private int year;

        public Student()
        {
        }
        public Student(int id, int yr,string name, string surname, string dob, string address)
        {
            this.id = id;
            this.name = name;
            this.surname = surname;
            this.dob = dob;
            this.address = address;
            this.year = yr;
        }

      
      public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public string Surname
        {
            get { return surname; }
            set { surname = value; }

        }

        public string DOB
        {
            get { return dob; }
            set { dob = value; }
        }

        public string Addr
        {
            get { return address; }
            set { address = value; }

        }
        public int Id
        {
            get { return id; }
            set { id = value; }
        }


        public int Year
        {
            get { return year; }
            set { year = value; }

        }


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>