【问题标题】:Creating a method in a class to update a list held within the class在类中创建一个方法来更新类中保存的列表
【发布时间】:2013-12-11 22:22:00
【问题描述】:

我正在尝试在名为“顾问”的课程中创建一个列表,该列表将包含由 1 个顾问实例提供建议的学生列表。我创建了一个学生课程,在那里没有问题,但是我在运行时的顾问课程中遇到错误。

class Advisers
{
    private string _Fname;
    private string _Lname;
    private string _Department;
    private List<Student> _StudentsAdvised;   

    //constructor
    public Advisers()
    {
        _Fname = "";
        _Lname = "";
        _Department = "";
        _StudentsAdvised = null;
    }

    public void AddToList(Student newStudent)
    {
        _StudentsAdvised.Add(newStudent);
    }

    public List<Student> StudentsAdvised
    {
        get { return _StudentsAdvised; }
        set { _StudentsAdvised = value; }
    }

    public string FirstName
    {
        get { return _Fname; }
        set { _Fname = value; }
    }

    public string LastName
    {
        get { return _Lname; }
        set { _Lname = value; }
    }

    public string Department
    {
        get { return _Department; }
        set { _Department = value; }
    }       
}

AddToList 方法是我在主窗体中调用的方法,用于将学生实例添加到 Adviser 类中的 StudentsAdvised 列表中。

private void stuAssignAdviBtn_Click(object sender, EventArgs e)
    {
        int stuIndex = stuLstbox.SelectedIndex;
        int advIndex = adviListBox.SelectedIndex;

        if (stuLstbox.SelectedIndex != -1 && adviListBox.SelectedIndex != -1)
        {
            string AdvLastName = AdviserList[advIndex].LastName;
            StudentList[stuIndex].AdviserLastName = AdvLastName;


            AdviserList[advIndex].AddToList(StudentList[stuIndex]);
        }
        else
        {
            MessageBox.Show("Select 1 student and 1 adviser.");
        }
    }

错误消息出现在 Adviser 类中:

public void AddToList(Student newStudent)
{
    _StudentsAdvised.Add(newStudent);
}

我该怎么做?

【问题讨论】:

  • 他们不会被劝告吗?
  • 更改 _StudentsAdvised = null;在您的构造函数中 _StudentsAdvised = new List();
  • List 可以是 BindingList...至少...或者从 bindinglist(或通用类)派生一个类,然后在您的顾问类中使用您需要的正确实现派生的从 bindinglist 类保存信息...

标签: c# list class methods


【解决方案1】:

我没有看到你在任何地方初始化你的List&lt;Student&gt; _StudentsAdvised

可以在类构造函数中这样做

//constructor
public Advisers()
{
    _Fname = "";
    _Lname = "";
    _Department = "";
    _StudentsAdvised = new List<Student>();
}

【讨论】:

  • 同意 - 你没有在构造函数中更新你的集合。您只是将其设为 null,因此当您尝试使用该对象时会引发 nullReferenceException
  • 我(菜鸟)对构造函数的理解是它初始化了值。我想将列表设为空,这就是我将其设置为 null 的原因。不过这行得通,谢谢!
【解决方案2】:

嗯,

public class Advisess : List<Student>
{
    private readonly Advisor advisor;

    private Advisess()
    {
    }

    public (IEnumerable<Student> adivsees, Advisor advisor)
        : base(advisees)
    {
        this.advisor = advisor;
    }

    public (Advisor advisor)
    {
        this.advisor = advisor;
    }

    public Advisor Advisor
    {
        get
        {
            return this.advisor;
        }
    }
}

【讨论】:

    猜你喜欢
    • 2020-04-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多