1

is something like the penultimate line here possible? I.E. referring to other properties within the declaration itself?

In my actual code, A and B are set to complex LINQ Enumerables (which is fine) and C could be something like A.Count():

class Thing {
    public int A;
    public int B;
    public int C;
}

Thing myThing = new Thing() {
    A = 1,
    B = 2,
    C = A+B
}

Just trying to find the most efficient way of constructing myThing.

rwalter
  • 891
  • 1
  • 7
  • 19
  • @Alex read post carefully, in his actual code A and B - enumerables – Viktor Lova Jul 10 '13 at 12:54
  • To be extra clear - the question I want answered is whether something like this is possible. Or, do I have no choice but to define property C _after_ initialising the object? – rwalter Jul 10 '13 at 12:57
  • possible duplicate of [Accessing properties from object initializer](http://stackoverflow.com/questions/11859553/accessing-properties-from-object-initializer) – sloth Jul 10 '13 at 12:57
  • Is `C` *always* going to be `A`+`B`? Ideally you could make `C` a readonly property which could encapsulate that logic. – James Jul 10 '13 at 12:59

5 Answers5

1

Consider this example:

int A = 0;
int B = 0;
int C = 0;
MyThing myThing = new MyThing() {
    A = 1,
    B = 2,
    C = A + B
}

// myThing.A == 1
// myThing.B == 2
// myThing.C == 0

So, no; it is not possible. Use any of the other answers instead.

Nolonar
  • 5,962
  • 3
  • 36
  • 55
1

Sadly, it's not possible to retrieve a property's value in the object's initializer.

Basuro
  • 1,084
  • 9
  • 12
1

I don't think A and B would be updated before you can reference them to use in C. The solution in my eyes is to simple assign A and B to variables before creating the Object:

var variable1 = 1
var variable2 = 2

Thing myThing = new Thing() {
    A = variable1,
    B = variable2,
    C = variable1+variable2
}

that way you can be sure both variables are changed before you use them for C

Edit: "do I have no choice but to define property C after initialising the object?" The problem is that A and B aren't initialised yet, so you can't use them to create C. But if you initialise them under another name(variable 1 and variable 2 in the above example) you can then go on to use those values to get C

0

Properties to the rescue (based on your example)

class Thing {
    public IEnumerable<someType> A { get; set; }
    public IEnumerable<someType> B { get; set; }
    public int C { get { return A.Count(); } }
}

You just need to set A and B, C will come out "on its own" based on the other two. Add null-checks and error handling as needed. In fact, C cannot be set at all in this sample, and it's correct because its value dependes on another property (here it's A).

Alex
  • 23,004
  • 4
  • 39
  • 73
0

Object initialiser syntax you're using is just a shortcut for assigning the values, so you could do this:

Thing myThing = new Thing() {
    A = 1,
    B = 2};
myThing.C =myThing.A + myThing.B;
Jon G
  • 4,083
  • 22
  • 27
  • You could also just as easily do `new Thing() { A = 1, B = 2, C = 1 + 2 };`. – James Jul 10 '13 at 13:08
  • I have accepted this answer because C is not always A+B, but could be A.Count() or anything else. Credit to @James too because he answered the question - the answers appears to be No, what I was trying is not possible! – rwalter Jul 10 '13 at 13:10
  • @rwalter not sure how it could be `A.Count()` because in your example `A` is an `int`, not a collection. Regardless, I think you understand that it can't be done as part of the initializer and there are various ways of setting the property (this being one). – James Jul 10 '13 at 13:16