Adding to a list from another class

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have a program which contains JuniorStudents and SeniorStudents. Ive 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 but when I want to view the new
senior student Im not getting the item. Can someone help me please? Here is the code.
<pre class="prettyprint" style=" 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()<br/> {<br/> for (int i = 0; i < studlist.Count; i++)<br/> {<br/> Console.Write(studlist.Id + "t");<br/> Console.Write(studlist.Year + "t");<br/> Console.Write(studlist.Name + "t");<br/> Console.Write(studlist.Surname + "t");<br/> Console.Write(studlist.DOB + "t");<br/> Console.Write(studlist.Addr);<br/> Console.WriteLine();<br/> }<br/> }
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; }

}
[/code]
<br/>


<br/>


<br/>



<br/>
<br/>

View the full article
 
Back
Top