C#列出访问对象并将名称添加到列表中

时间:2013-11-26 16:02:31

标签: c# list permissions

我正在尝试将列表中的所有名称都显示在列表框中。这是我的代码。

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

    }

}

}

我的大学课程:

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

        }
    }

}

}

学生班:

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

    }
}

}

我正在尝试按下btnlst_click并输出已输入的所有名称的列表。

我知道我需要使用类似:foreach(学生在拥有)这样的东西,但是我没有权限从主窗口类中进行操作而且我不确定如何将它从大学课程传递到主窗口要放在字符串中。有人可以提供建议吗?

1 个答案:

答案 0 :(得分:2)

你必须定义你的方法,让它返回一个学生名列表。

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

这大致转化为

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

这个小的LINQ表达式将选择学生的所有名字并将其返回给您使用。请注意List<string>返回语句,而不是void

在您的主要表单类中:

myListBox.DataSource = someUniversity.GetStudents():

我不熟悉C#中的GUI开发,所以这个分配看起来可能不同。

记住命名约定:方法是CamelCased!