1

Is something like the following possible or do you have to return the list and assign it afterwards? I get object reference not set to instance of an object.

public class MyCollection
{
    public List<SomeObject> Collection { get; set; }

    public List<SomeObject> CreateCollection()
    {
        // Is there a way to set the Collection property from here???
        this.Collection.Add(new SomeObject()
        {
            // properties
        });

    }
}

...

MyCollection collection = new MyCollection();                    
collection.CreateCollection();
gunr2171
  • 16,104
  • 25
  • 61
  • 88
user120455
  • 35
  • 7
  • First, this will not compile. Your function `CreateCollection()` must return a `List`, which it does not, and you are not using in your calling code. – gunr2171 Jun 02 '14 at 20:07
  • you need to instantiate the list this.Collection = new List(); and add return before – TotPeRo Jun 02 '14 at 20:08

4 Answers4

2

Yes, you can use an object initializer:

public List<SomeObject> CreateCollection()
{
    // You may want to initialize this.Collection somehere, ie: here
    this.Collection = new List<SomeObject>();

    this.Collection.Add(new SomeObject
    {
        // This allows you to initialize the properties
        Collection = this.Collection
    });
    return this.Collection;
}

Note that this will still potentially have an issue - you are never initializing this.Collection in any code you're displaying. You will need to initialize it to a proper collection in your constructor or via some other mechanism.

It is also an odd choice to have a "Create" method that initializes the local variable and returns a List<T>. Typically, you'd do one or the other. A more common approach would be to place this code within the constructor:

public class MyCollection
{
    public IList<SomeObject> Collection { get; private set; } // The setter would typically be private, and can be IList<T>!

    public MyCollection()
    {
        this.Collection = new List<SomeObject>();
        this.Collection.Add(new SomeObject
        {
            Collection = this.Collection
        });
    } 
}

You could then use it via:

MyCollection collection = new MyCollection();                    
var object = collection.Collection.First(); // Get the first element

That being said, in general, there is no real reason to make a custom class for a collection like this in most cases. Just using a List<SomeObject> directly is typically sufficient.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
1

It's completely possible - you just have to instantiate it first, before you can use it:

public List<SomeObject> CreateCollection()
{
    this.Collection = new List<SomeObject>(); // this creates a new list - the default if you just define a list but don't create it is for it to remain null
    this.Collection.Add(new SomeObject()
    {
        // whatever
    });
}

Of course, as pointed out in a comment, if you want that function to return a list, it would have to actually return the list. Presumably you mean public void CreateCollection(), though, since that was your question, whether you actually had to return a list (answer: no).

neminem
  • 2,658
  • 5
  • 27
  • 36
  • I would not check for null before adding elements, I guess that "CreateCollection" means Create, not append elements to an existing one ? – quantdev Jun 02 '14 at 20:08
  • You need to add return before this.Collection.Add ...... because the method return List – TotPeRo Jun 02 '14 at 20:11
0

You must initialize this.Collection before adding elements into it.

public List<SomeObject> CreateCollection()
{
    this.Collection = new List<SomeObject>();
    this.Collection.Add(new SomeObject()
    {
        // properties
    });

}
quantdev
  • 23,517
  • 5
  • 55
  • 88
0

You can use a list initializer in this case:

public class Person
{
    public string Name { get; set; }
    public string Firstname { get; set; }
}
class Program
{
    public static List<Person> Collection { get; set; }

    public static List<Person> CreateCollection()
    {

        return new List<Person>()
        {
            new Person() { Name = "Demo", Firstname = "Demo1"},
            new Person() { Name = "Demo", Firstname = "Demo1"},
        };

    }

    static void Main(string[] args)
    {
         Collection = CreateCollection();
    }
}
Complexity
  • 5,682
  • 6
  • 41
  • 84
  • You might as well include the few converted lines in the `Main` function. This is so the OP knows what to call and how. – gunr2171 Jun 02 '14 at 20:13