3

I need to create a class that also initializes two event lists to new empty lists. I'm not sure if that is what is being asked of me, but I know how to create a list and how to create a constructor. I created 2 lists, and now I should create the constructor. Here is one of my lists:

List<Person> organize = new List<Person>();

How do I initialize the two event lists in the constructor to new lists?

DanM7
  • 2,203
  • 3
  • 28
  • 46
Shonna
  • 279
  • 2
  • 3
  • 5
  • Need some more information. As it stands, yes, it makes no sense. post a larger code block? – Russell Steen Mar 22 '10 at 21:00
  • 3
    language barrier + homework question = fail? – Allen Rice Mar 22 '10 at 21:01
  • Somebody else tagged it as homework...you never know. – Justin Niessner Mar 22 '10 at 21:04
  • well we created the 2 lists. they should be read only properties in the class. Now we have to create a constructor that i guess re-initialize the two lists to new empty lists... some of the responses i saw looks like what i need to do though! thanks .. sorry if i suck at explaining. – Shonna Mar 22 '10 at 21:04
  • i understood how to do whats posted by people, i just was not sure if this is what was being asked of me. – Shonna Mar 22 '10 at 21:07

5 Answers5

11

Based on what I can gather from your question, you have a class with two Lists.

Your requirements say that inside your class, you need to initialize the Lists to empty lists.

Below is the example (the only difference is that I never initialize the Lists when they declared, but in the class constructor instead):

public class YourClass
{
    List<Person> organize;
    List<Person> anotherOrganize;

    // constructor
    public YourClass()
    {
        // initialize the two lists to empty lists
        organize = new List<Person>();
        anotherOrganize = new List<Person>();
    }
}
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
2

If your list is declared as a field (member variable directly inside the class) and it's initialized at its declaration, you shouldn't to reinitialize it in the constructor. The initialization expression will get moved to the constructor by the compiler automatically.

Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
2

If this is homework, your instructor is probably telling you to initialize the lists inside of the constructor instead of at the declaration point.

class MyClass
{
    List<Person> organize;

    public MyClass()
    {
        this.organize = new List<Person>();
    }
}
Nick Larsen
  • 18,631
  • 6
  • 67
  • 96
0
List<Person> organize = new List<Person>();

This will create an empty list. Can you be more specific? I feel like some details are missing.

Perhaps you mean something like this?

 public class TestClass
 {
      List<Person> personList;

      public TestClass()
      {
           personList = new List<Person>();
      }

 }
Bryan Denny
  • 27,363
  • 32
  • 109
  • 125
0

I think what you're asking is why the constructor to your class should instantiate the two lists (?). You're right that

List<Person> organize = new List<Person>();

will instantiate the list. But so will:

List<Person> organize;
public MyClass()
{
organize = new List<Person>();
}

Am I understanding the question?

hackerhasid
  • 11,699
  • 10
  • 42
  • 60