簡體   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