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?

5
  • Need some more information. As it stands, yes, it makes no sense. post a larger code block? Commented Mar 22, 2010 at 21:00
  • 3
    language barrier + homework question = fail? Commented Mar 22, 2010 at 21:01
  • Somebody else tagged it as homework...you never know. Commented Mar 22, 2010 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. Commented Mar 22, 2010 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. Commented Mar 22, 2010 at 21:07

5 Answers 5

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

Comments

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.

Comments

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

Comments

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

Comments

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?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.