Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to mix object initializer and collection initializer?

I define an collection initializer with IEnumerable as instructed here: http://msdn.microsoft.com/en-us/library/bb384062.aspx

Now I'm able to create objects within my collection initializer and they are added wih my Add() method like so:

class ArrangedPanel : RectElement
{
    private List<RectElement> arrangedChildren = new List<RectElement>();
    public int Padding = 2;

    public void Add(RectElement element)
    {
        arrangedChildren.Add(element);
        //do custom stuff here
    }

    public IEnumerator GetEnumerator()
    {
        return arrangedChildren.GetEnumerator();
    }
}

// Somewhere
debugPanel.Add(new ArrangedPanel() 
{ 
    new ButtonToggle(),
    new ButtonToggle()
});

However, if I try to set a property, such as my "Padding" field, I get an error on the collection initializers.

debugPanel.Add(new ArrangedPanel() 
{ 
    Padding = 5,
    new ButtonToggle(),
    new ButtonToggle()
});

Is it possible to set both collection initializers and object initializers?

like image 261
jsmars Avatar asked Oct 04 '11 11:10

jsmars


People also ask

What is object initialization why it is required?

In object initializer, you can initialize the value to the fields or properties of a class at the time of creating an object without calling a constructor. In this syntax, you can create an object and then this syntax initializes the freshly created object with its properties, to the variable in the assignment.

What is object initializer?

An object initializer is an expression that describes the initialization of an Object . Objects consist of properties, which are used to describe an object. The values of object properties can either contain primitive data types or other objects.

What is collection initializer?

Object and collection initializers offer more concise syntax. Besides, initializers are useful in multi-threading. Object initializers are used to assign values to an object's properties or fields at creation time without invoking the constructor.

How are Initializers executed in C#?

Initializers execute before the base class constructor for your type executes, and they are executed in the order in which the variables are declared in your class. Using initializers is the simplest way to avoid uninitialized variables in your types, but it's not perfect.


1 Answers

I had a similar problem. The closest one can obviously get, is to add a property to the class which allows the collection initializer access:

In ArrangedPanel:

public ArrangedPanel Container {
   get { return this; }
}

And in code:

debugPanel.Add(new ArrangedPanel() 
{ 
    Padding = 5,
    Container = {
        new ButtonToggle(),
        new ButtonToggle()
    }
});

not too bad, I guess ?

@Edit: according to the comment by @Tseng I changed the return value of the new property to return the ArrangedObject itself instead of its List<RectElement> member. This way the ArrangedPanel.Add method is called and any (potentially more complex) logic in it is reused.

@Edit2: renamed the property ('Children' -> 'Container') in the hope that the new name better reflects the new meaning.

like image 107
Haymo Kutschbach Avatar answered Oct 14 '22 22:10

Haymo Kutschbach