CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2012
    Posts
    39

    Adding To A List From Another Class

    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:

    Code:
    public class JuniorStudentsClass
    002
        {
    003
       public List<Student> mystudent = new List<Student>();
    004
    ublic void Addstudent()
    005
            {
    006
                Console.Write("enter students id:");
    007
                s.Id = Int32.Parse(Console.ReadLine());
    008
                Console.Write("enter students year:");
    009
                s.Year = Int32.Parse(Console.ReadLine());
    010
                Console.Write("enter name:");
    011
                s.Name = Console.ReadLine();
    012
                Console.Write("enter surname:");
    013
                s.Surname = Console.ReadLine();
    014
                Console.Write("enter dob:");
    015
                s.DOB = Console.ReadLine();
    016
                Console.Write("enter address:");
    017
                s.Addr = Console.ReadLine();
    018
                mystudent.Add(new Student(s.Id, s.Year, s.Name, s.Surname, s.DOB, s.Addr));
    019
     
    020
            }
    021
     public void PromoteStudents(int index)
    022
            {
    023
                SearchItem(index);
    024
                Console.WriteLine("current record:");
    025
                Console.WriteLine("id is:" + mystudent[index].Id);
    026
                Console.WriteLine("year is:" + mystudent[index].Year);
    027
                Console.WriteLine("name is:" + mystudent[index].Name);
    028
                Console.WriteLine("surname is:" + mystudent[index].Surname);
    029
                Console.WriteLine("dob is:" + mystudent[index].DOB);
    030
                Console.WriteLine("address is:" + mystudent[index].Addr);
    031
                Console.WriteLine("enter new year of student to promote");
    032
                s.Year = Int16.Parse(Console.ReadLine());
    033
                if (((s.Year == 7)) || ((s.Year == 8)))
    034
                {              
    035
                    sc.List.Add(new Student(mystudent[index].Id, s.Year, mystudent[index].Name, mystudent[index].Surname, mystudent[index].DOB, mystudent[index].Addr));
    036
                    Console.WriteLine("student promoted to senior student");
    037
                }
    038
                else
    039
                {
    040
                    mystudent.Add(new Student(mystudent[index].Id, s.Year, mystudent[index].Name, mystudent[index].Surname, mystudent[index].DOB, mystudent[index].Addr));
    041
                    Console.WriteLine("student promoted to next year");
    042
                }
    043
            }
    044
     
    045
     
    046
            public int SearchItem(int search)
    047
            {
    048
                int found = -1;
    049
                if (mystudent != null)
    050
                {
    051
                    foreach (Student st in mystudent)
    052
                    {
    053
                        found++;
    054
                        if (Student.Equals(search, st))
    055
                        {
    056
                            break;
    057
                        }
    058
     
    059
                    }
    060
                }
    061
                return found;
    062
            }
    063
     
    064
    public class SeniorStudentsClass
    065
        {
    066
            Student s = new Student();;
    067
            public List<Student> studlist = new List<Student>();
    068
     
    069
            public List<Student> List {
    070
                get { return studlist; }
    071
                set { studlist = value; }
    072
            }
    073
     
    074
            public void Addstudent()
    075
            {
    076
                Console.Write("enter students id:");
    077
                s.Id = Int32.Parse(Console.ReadLine());
    078
                Console.Write("enter students year:");
    079
                s.Year = Int32.Parse(Console.ReadLine());
    080
                Console.Write("enter name:");
    081
                s.Name = Console.ReadLine();
    082
                Console.Write("enter surname:");
    083
                s.Surname = Console.ReadLine();
    084
                Console.Write("enter dob:");
    085
                s.DOB = Console.ReadLine();
    086
                Console.Write("enter address:");
    087
                s.Addr = Console.ReadLine();
    088
                studlist.Add(new Student(s.Id, s.Year, s.Name, s.Surname, s.DOB, s.Addr));  
    089
     
    090
            }  public void ViewStudents()
    091
            {
    092
                for (int i = 0; i < studlist.Count; i++)
    093
                {
    094
                    Console.Write(studlist[i].Id + "\t");
    095
                    Console.Write(studlist[i].Year + "\t");
    096
                    Console.Write(studlist[i].Name + "\t");
    097
                    Console.Write(studlist[i].Surname + "\t");
    098
                    Console.Write(studlist[i].DOB + "\t");
    099
                    Console.Write(studlist[i].Addr);
    100
                    Console.WriteLine();
    101
                }
    102
            }
    103
     
    104
      public class Student
    105
        {
    106
            Marks mrk = new Marks();
    107
            private int id;
    108
            private string name;
    109
            private string surname;
    110
            private string dob;
    111
            private string address;
    112
            private int year;
    113
     
    114
            public Student()
    115
            {
    116
            }
    117
            public Student(int id, int yr,string name, string surname, string dob, string address)
    118
            {
    119
                this.id = id;
    120
                this.name = name;
    121
                this.surname = surname;
    122
                this.dob = dob;
    123
                this.address = address;
    124
                this.year = yr;
    125
            }
    126
     
    127
           
    128
          public string Name
    129
            {
    130
                get { return name; }
    131
                set { name = value; }
    132
            }
    133
     
    134
            public string Surname
    135
            {
    136
                get { return surname; }
    137
                set { surname = value; }
    138
     
    139
            }
    140
     
    141
            public string DOB
    142
            {
    143
                get { return dob; }
    144
                set { dob = value; }
    145
            }
    146
     
    147
            public string Addr
    148
            {
    149
                get { return address; }
    150
                set { address = value; }
    151
     
    152
            }
    153
            public int Id
    154
            {
    155
                get { return id; }
    156
                set { id = value; }
    157
            }
    158
     
    159
     
    160
            public int Year
    161
            {
    162
                get { return year; }
    163
                set { year = value; }
    164
     
    165
            }

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Adding To A List From Another Class

    I note your later post as well re same topic. If you care to post the complete code that complies and runs and produces the problem I'll have a look at it.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured