5 Replies - 388 Views - Last Post: 12 December 2012 - 06:53 AM Rate Topic: -----

#1 mel_ga  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 32
  • Joined: 18-January 10

Adding to a list from another class

Posted 12 December 2012 - 05:53 AM

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; }

        }



Is This A Good Question/Topic? 0
  • +

Replies To: Adding to a list from another class

#2 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 314
  • View blog
  • Posts: 490
  • Joined: 13-June 12

Re: Adding to a list from another class

Posted 12 December 2012 - 06:13 AM

I doubt that even compiles.

Firstly, in the AddStudent() method, you're referencing a variable named "s" which doesn't exist. Also, "s" is a terrible object name. I assume it's of Student class, but it could be anything. Why not name it "student"?
If it is a Student, then line 18 doesn't need to instantiate a new Student. Just use mystudent.Add(s);.

Next, line 23 makes no sense. You are using a method to get the index of a student in a List, which returns the index. But you're providing it an index, not getting its return value, then using the same index you've provided to access a particular student object from "mystudent". Again you're referencing "s" and "sc"...

That's all I'm willing to point out right now. You need to think carefully about what you're doing, and plan it. This looks like you've tried different things and taken chunks out when they didn't work.
Was This Post Helpful? 1
  • +
  • -

#3 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 362
  • View blog
  • Posts: 1,539
  • Joined: 08-April 09

Re: Adding to a list from another class

Posted 12 December 2012 - 06:13 AM

Suggestion:
Why didn't you made class Student and then Junior class and senior can inherit the Student class.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5678
  • View blog
  • Posts: 12,216
  • Joined: 02-June 10

Re: Adding to a list from another class

Posted 12 December 2012 - 06:16 AM

Quote

uniorStudents and SeniorStudents. I've made seperate classes and in each class there is a list.


Your design here is off a bit. You don't want a list of students inside a class that defines what one JuniorStudent is. That would be like having a class that defines what a single Car is, but then having a list inside for the entire invetory of the car sales lot.


You probably want something more like this

List<Student> myStudents = new List<Student>;


class Student
{
   // shared properties like name
}

class JuniorStudent : Student
{
   // Things specific to a Junior
}

class SeniorStudent : Student
{ 
   / /Things specific to a Senior
}


Have a read through the Objects tutorial linked in my signature block for more.
Was This Post Helpful? 1
  • +
  • -

#5 mel_ga  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 32
  • Joined: 18-January 10

Re: Adding to a list from another class

Posted 12 December 2012 - 06:36 AM

My problem is that I can't view the new senior student when I call the viewStudent method. Any suggestions on how it would work. I know that my design is not so good but that's not the problem that I have
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5678
  • View blog
  • Posts: 12,216
  • Joined: 02-June 10

Re: Adding to a list from another class

Posted 12 December 2012 - 06:53 AM

"I can't drive from LA to San Francisco in 2 hours. I know my engine isn't running right, but that's not my problem."

The simple fact is that if the design is poor and confusing and not well thought out then you can't really think your way through how to code to make use of it. And *we* can't figure out how its supposed to work, much less how to make it do what you want.

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        } 


You don't even need an 's' in this. Just make a new student right off the bat. Because objects are reference types so you keep pointing all your new things at the same memory address of your one existing object: s.


You can keep throwing stuff against the wall to see what sticks... Or you can re-read the chapter in your text book and work through the tutorial I pointed you to. But either way you need to stop banging on the keyboard until you can some understanding and comprehension of the material and the concepts of making instances of objects.

I urge you to stop. Re-read. Do the tutorial. Then start fresh with version 2 of this project. Don't try to fix this mess, that's just more work than starting over.

If you start fresh with better design that takes all these mistakes into account you'll find you have a much better product.

This post has been edited by tlhIn`toq: 12 December 2012 - 06:52 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1