0

我有一个有两个构造函数的类库。第一个构造函数接受两个参数,第二个接受三个参数。下面是我的类库代码。编写代码比尝试解释它更容易。

public class Student
    {
        public string name;
        public string course;
        public MyDate bday;

        public Student(string name, string course)
        {
            this.name = name;
            this.course = course;
        }

        public Student(string name, string course, MyDate bday)
        {
            this.name = name;
            this.course = course;
            this.bday = bday;
        }

MyDate 库有另一个构造函数,它接受三个参数,即生日的日期、月份和年份。现在我有一个包含 3 个列表框的表单,在第三个列表框上我将显示生日。我在代码中声明了生日(就像我在下面显示的那样)现在我在如何显示它时遇到了问题。

MyDate[] bd = new MyDate[5] {  new MyDate(29, 3, 1990),
                                       new MyDate(30, 1, 1988),
                                       new MyDate(9, 6, 1987),
                                       new MyDate(2, 4, 1989),
                                       new MyDate(17, 8, 1986),
        };
        Student[] s = new Student[5] { new Student("John", "BSCS"),
                                       new Student("Paul", "BSIT"),
                                       new Student("George", "BSCP"),
                                       new Student("Jane", "BSCS"),
                                       new Student("May", "BSIT")
        };

谁能告诉我我该怎么做?我试过这个Student[] s = new Student[5] { new Student("John", "BSCS", bd[0])等等,但它给了我错误。我知道这是一个初学者的问题,我是一个初学者。谢谢你。

编辑:初始化是在 form1.cs 中完成的。

4

4 回答 4

1

根据错误,您正在尝试使用另一个非静态字段初始化一个字段(成员数据);你不能那样做。最简单的解决方法是将初始化代码移至构造函数。

所以你的代码是

partial class Form
{
   MyDate[] bd = ...;
   Student[] s;

   public Form()
   {
      InitializeComponent();

      s = ...;
   }
}

您还应该链接您的Student构造函数,为什么您有自己的日期类而不是使用System.DateTime?您还应该使用自动属性而不是public字段。

public class Student
    {
        public string name { get; set; }
        public string course { get; set; }
        public DateTime bday { get; set; }

        public Student(string name, string course)
        {
            this.name = name;
            this.course = course;
        }

        public Student(string name, string course, DateTime bday)
        : this(name, course)
        {
            this.bday = bday;
        }
}
于 2013-02-28T03:23:50.563 回答
1

你可以在这里看到:

您应该在构造函数下设置字段初始值设定项。

class TestClass
{
    MyDate[] bd;
    Student[] s; 

    public TestClass()
    {
         bd = new MyDate[5] {  new MyDate(29, 3, 1990),
                                   new MyDate(30, 1, 1988),
                                   new MyDate(9, 6, 1987),
                                   new MyDate(2, 4, 1989),
                                   new MyDate(17, 8, 1986),
                            };

         s = new Student[5] { new Student("John", "BSCS"),
                                   new Student("Paul", "BSIT"),
                                   new Student("George", "BSCP"),
                                   new Student("Jane", "BSCS"),
                                   new Student("May", "BSIT")
                             };
    }
}

基本上是说你需要在构造函数中设置这个变量。

于 2013-02-28T03:22:00.307 回答
0

这是做同样事情的一种方法:

    Student[] s = new Student[5];
    s[0] = new Student("John", "BSCS", bd[0]);
    s[1] = new Student("Bill", "BSCS", bd[1]);
于 2013-02-28T03:22:39.120 回答
0

我必须在这里遗漏一些东西,因为我无法重新创建这里讨论的错误。以下代码对我来说很好(.NET 4.0)

public class MyDate
{
    public MyDate(int date, int month, int year)
    {
        this.date = date;
        this.month = month;
        this.year = year;
    }

    public int date;
    public int month;
    public int year;
}

public class Student
{
    public string name;
    public string course;
    public MyDate bday;

    public Student(string name, string course)
    {
        this.name = name;
        this.course = course;
    }

    public Student(string name, string course, MyDate bday)
    {
        this.name = name;
        this.course = course;
        this.bday = bday;
    }
}

..我能够像下面这样初始化对象..

            MyDate[] bd = new MyDate[5]
                          {
                              new MyDate(29, 3, 1990),
                              new MyDate(30, 1, 1988),
                              new MyDate(9, 6, 1987),
                              new MyDate(2, 4, 1989),
                              new MyDate(17, 8, 1986),
                          };

        Student[] s = new Student[5]
                          {
                              new Student("John", "BSCS", bd[0]),
                              new Student("Paul", "BSIT", bd[1]),
                              new Student("George", "BSCP", bd[2]),
                              new Student("Jane", "BSCS", bd[3]),
                              new Student("May", "BSIT", bd[4])
                          };

你到底什么时候收到这个错误?

于 2013-02-28T04:33:13.547 回答