繁体   English   中英

如何在C#中声明和显示变量?

[英]How to declare and display variable in C#?

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

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")
        };

任何人都可以告诉我应该怎么做? 我试过这个学生[] s =新学生[5] {新学生(“John”,“BSCS”,bd [0])等等,但它给了我错误。 我知道这是一个初学者问题,我是初学者。 谢谢。

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

你可以在这里看到

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

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")
                             };
    }
}

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

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

所以你的代码就是

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;
        }
}

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

    Student[] s = new Student[5];
    s[0] = new Student("John", "BSCS", bd[0]);
    s[1] = new Student("Bill", "BSCS", bd[1]);

我必须在这里遗漏一些东西,因为我无法重现这里讨论的错误。 以下代码适合我(.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])
                          };

你什么时候得到这个错误?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM