0

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. Now I have a form which contains 3 listbox and on the third listbox I will be displaying the birthdays. 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. I know this is a beginners question and I am a beginner. Thank you.

Edit: The initialization was done in the form1.cs.

4
  • 3
    Please share with us what error you are actually recieving. Commented Feb 28, 2013 at 3:15
  • It says "A field initializer cannot reference the non-static field blah blah blah Commented Feb 28, 2013 at 3:17
  • 1
    Error speaks for itself. You cant use bd[0] in your field initializer. Move it into the constructor. Commented Feb 28, 2013 at 3:20
  • I'm sorry if this is a silly question, but what should I move to the constructor? Commented Feb 28, 2013 at 3:21

4 Answers 4

1

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? You also should use automatic properties rather than public fields.

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;
        }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Dan. I will try to this. We are told to create our own MyDate class and that our class libraries should only contain the constructors and the variable's declared. Initializing those variables should be done in the form. (Hope you get what I mean)
1

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.

2 Comments

It's the same on your answer. I've just included the bd array initialization at the constructor, so next if she's going to look for it. It's just 2 lines up to it.
Hello. We are told to do all our initialization in the form.cs and not in the constructor or in class library. I tried Dan's solution but my problem us using bd[0] and so on.
0

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

2 Comments

Thanks Kal_Torak but that doesn't work either. It gives me all sorts of error. :-(
Oh, I was assuming that code was inside a method, my bad. See Freddie's answer
0

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)

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?

4 Comments

The error is in the bd[0]. Did u initialize this in the form1.cs? Coz that's where I used this code and that's where i get the error. Thank u
Hi James. May I ask where did you put the initialization? Coz I am still getting the same error message. And how will I call it to display on the listbox? Thanks u
@SophiaIsabella, If you are asking where i did 'MyDate[] bd = new MyDate[5]{...}', that in a button click of my Winform test application along with 'Student[] s = new Student[5]{...}'.
Thanks James. I wrote mine as a global declaration that's why I get all the errors. But I fixed it now. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.