Using collection initializer syntax on custom types?
Quick fix : Make your own List
type with an Add
overload that takes multiple arguments:
class LookupList : List<LookupItem> {
public void Add(int Param1, int Param2, ... sometype ParamX) {
this.Add(new LookupItem() { Param1 = Param1, Param2 = Param2, ... ParamX = ParamX });
}
}
Now works exactly as you want:
private static LookupList _lookupTable = new LookupList() {
{1,2,3,4},
{2,7,6,3}
};
More fundamental answer:
You're mixing up object initializers and collection initializers. Put simply:
Object initializers are a syntactic trick that, in the background call the property set methods for each named property with the value specified.
Collection initializers are a syntactic trick that, in the background:
- For an
Array
type: Fill the array with the items - For any other type, which must implement
IEnumerable
: Call theAdd
method for each sub-bracketed set.
That is all there is to it. Consider for example the following hack:
public class Hack : IEnumerable {
public int LuckyNumber { get; set; }
public double Total { get; private set; }
public void Add(string message, int operand1, double operand2, double operand3) {
Console.WriteLine(message);
this.Total += operand1 * operand2 - operand3;
}
public IEnumerator GetEnumerator() { throw new NotImplementedException(); }
}
class Program {
static void Main(string[] args) {
Hack h1 = new Hack() {
{ "Hello", 1, 3, 2},
{ "World", 2, 7, 2.9}
};
Console.WriteLine(h1.Total);
Hack h2 = new Hack() { LuckyNumber = 42 };
Console.WriteLine(h2.LuckyNumber);
}
}
You should never do this in a real program, but I hope examining this example and the results, especially if you debug step through it, will help you understand the initializers clearly and choose a good solution for your current scenario.
You're trying to use a collection initializer on the list itself, not on your type:
// Note the "new List<...>" part - that specifies what type the collection
// initializer looks at...
private static List<LookupItem> _lookupTable = new List<LookupItem>()
{
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
}
So it's looking for an Add
method with four parameters on List<T>
, and that doesn't exist.
You would have to implement your own collection class to use a custom collection initializer. While you're using List<T>
, you're stuck with the constructor calls you've got.
I think you need to make a custom collection instead of List. Call it LookupItemTable, for example. Give that collection an Add(int, int, float, float) method and have it implement IEnumerable. For example:
class LookupItem
{
public int a;
public int b;
public float c;
public float d;
}
class LookupItemTable : List<LookupItem>
{
public void Add(int a, int b, float c, float d)
{
LookupItem item = new LookupItem();
item.a = a;
item.b = b;
item.c = c;
item.d = d;
Add(item);
}
}
private static LookupItemTable _lookupTable = new LookupItemTable {
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 }
};
I've now tried the above code and it seems to work for me.