简体   繁体   English

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

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

I have a class library that has two constructor. 我有一个有两个构造函数的类库。 The first constructor accepts two arguments and the second accepts three. 第一个构造函数接受两个参数,第二个接受三个。 Below is my class library code. 下面是我的类库代码。 Easier to put the code than try to explain it. 比试图解释它更容易放置代码。

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

The MyDate library has another constructor that accepts three arguments which would be the date, month and year of the birthday. MyDate库有另一个构造函数,它接受三个参数,即生日的日期,月份和年份。 Now I have a form which contains 3 listbox and on the third listbox I will be displaying the birthdays. 现在我有一个包含3个列表框的表单,在第三个列表框中我将显示生日。 I declare the birthdays in the code (just like i showed below) now I'm having problem on how to display it. 我在代码中声明了生日(就像我在下面展示的那样),现在我遇到了如何显示它的问题。

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

Can anyone please tell me how i should do this? 任何人都可以告诉我应该怎么做? I tried this Student[] s = new Student[5] { new Student("John", "BSCS", bd[0]) and so on but it gives me error. 我试过这个学生[] s =新学生[5] {新学生(“John”,“BSCS”,bd [0])等等,但它给了我错误。 I know this is a beginners question and I am a beginner. 我知道这是一个初学者问题,我是初学者。 Thank you. 谢谢。

Edit: The initialization was done in the form1.cs. 编辑:初始化在form1.cs中完成。

You can see here: 你可以在这里看到

You should set the field initializer under your constructor. 您应该在构造函数下设置字段初始值设定项。

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

It's basically saying that you need to set this variable in the constructor. 它基本上是说你需要在构造函数中设置这个变量。

Based on the error, you're trying to initialize a field (member data) with the another non-static field; 根据错误,您尝试使用另一个非静态字段初始化字段(成员数据); you can't do that. 你不能这样做。 The simplest fix is to move your initialization code to the constructor. 最简单的解决方法是将初始化代码移动到构造函数中。

So your code would be 所以你的代码就是

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

   public Form()
   {
      InitializeComponent();

      s = ...;
   }
}

You also should chain your Student constructors, and why do you have your own date class instead of using System.DateTime ? 你也应该链接你的Student构造函数,为什么你有自己的日期类而不是使用System.DateTime You also should use automatic properties rather than public fields. 您还应该使用自动属性而不是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;
        }
}

Here's one way to do the same thing: 这是做同样事情的一种方法:

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

I must be missing something here, because i am not able to recreate the error discussed here. 我必须在这里遗漏一些东西,因为我无法重现这里讨论的错误。 The following code just works fine for me (.NET 4.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;
    }
}

..and i am able to initialize the objects like below.. ..我能够初始化下面的对象..

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

When exactly are you getting this error? 你什么时候得到这个错误?

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

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