Sean
Sean

Reputation: 163

C# List access object and add name to list

I'm trying to get all the names in a list and display them in a listbox. Here's my 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)
    {

    }

}

}

My University class:

namespace UniRecords

class University
{
    //Creates a list of students that University owns
    private List<Student> owns = new List<Student>();
    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)
        {

        }
    }

}

}

Student class:

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()
    {

    }
}

}

I'm trying to have btnlst_click be pressed and get output a list of all the names that have been inputted.

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?

Upvotes: 1

Views: 3847

Answers (1)

Jeroen Vannevel
Jeroen Vannevel

Reputation: 44439

You have to define your method to have it return a list of studentnames.

public List<string> GetStudents(){
    return owns.Select(x => x.Name).ToList();
}

This roughly translates to

public List<string> GetStudents(){
    var result = new List<String>();
    foreach(var student in owns) {
         result.add(student.Name);
    } 
    return result;
}

This small LINQ expression will select all the names of the students and return it for you to use. Notice the List<string> return statement instead of void.

And in your main form class:

myListBox.DataSource = someUniversity.GetStudents():

I'm not familiar with GUI development in C#, so this assignment might look different.

Keep naming conventions in mind: methods are CamelCased!

Upvotes: 2

Related Questions