title: C# List access object and add name to list
body: <p>I'm trying to get all the names in a list and display them in a listbox. Here's my code.</p>
  
  <pre><code>namespace UniRecords
  
  public partial class MainWindow
  {
      private University uni = new University(); //Creates a new University object
      public MainWindow()
      {
          InitializeComponent();
      }
  
      private void btnadd_Click(object sender, RoutedEventArgs e)
      {
          Student newStudent = new Student(txtname.Text, txtaddress.Text, txtmatric.Text,txtcourse.Text); //Calls the student constructor to construct a student object
          uni.ownsStudent(newStudent); //Calls the newStudent method in the University class
      }
  
      private void btnshow_Click(object sender, RoutedEventArgs e)
      {
          uni.showStudents(); //calls the showStudents method
      }
  
      private void btnlist_Click(object sender, RoutedEventArgs e)
      {
  
      }
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>My University class: </p>
  
  <pre><code>namespace UniRecords
  
  class University
  {
      //Creates a list of students that University owns
      private List&lt;Student&gt; owns = new List&lt;Student&gt;();
      public University()
      {
  
      }
      public void ownsStudent(Student newStudent)
      {
          owns.Add(newStudent);//Adds a new student to the list
      }
      public void showStudents()
      {
          foreach (Student s in owns)
          {
              System.Windows.MessageBox.Show(s.printDetails()); //Prints out details of each student individually 
          }
      }
      public void getStudents()
      {
          foreach (Student s in owns)
          {
  
          }
      }
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Student class: </p>
  
  <pre><code>namespace UniRecords
  
  class Student
  {
      private string name;
      private string dob;             //Date of birth 
      private string course;
      private string matric;
      private string address;
  
      //Constructor
      public Student(string myname, string myaddress, string mymatric, string mycourse)
      {
          Name = myname;
          Address = myaddress;
          Matric = mymatric;
          Course = mycourse;
      }
  
      //Uses get and set to make sure that the variables are kept private
      public string Name
      {
          get { return name; }
          set { name = value; }
      }
  
      public string Dob
      {
          get { return dob; }
          set { dob = value; }
      }
      public string Course
      {
          get { return course; }
          set { course = value; }
      }
      public string Matric
      {
          get { return matric; }
          set { matric = value; }
      }
      public string Address
      {
          get { return address; }
          set { address = value; }
      }
  
      public string printDetails()
      {
          return "student is called " + Name + " " + Address + " " + Matric + " " + Course;
      }
      public void listNames()
      {
  
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I'm trying to have btnlst_click be pressed and get output a list of all the names that have been inputted. </p>
  
  <p>I know I need to use something like: foreach (Student s in owns) but I don't have permissions to do it from the mainwindow class and I'm not sure how I could pass it from the University class to mainwindow to be put in the string. Can someone offer advice?</p>