0

I would like to reference a property on an object within an object initializer. The problem is that the variable does not yet exist, so I cannot reference it like normal (object.method). I do not know if there is a keyword to reference the object in creation during the object initialization.

When I compile the following code I get the error - 'The name 'Width' does not exist in the context. I understand why I get this error, but my question is: Is there any syntax to do this?

public class Square
{
    public float Width { get; set; }
    public float Height { get; set; }
    public float Area { get { return Width * Height; } }
    public Vector2 Pos { get; set; }

    public Square() { }
    public Square(int width, int height) { Width = width; Height = height; }
}

Square mySquare = new Square(5,4)
{
    Pos = new Vector2(Width, Height) * Area
};

I would like to reference the properties "Width", "Height", and "Area" in terms of "mySquare".

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ostler.c
  • 3,282
  • 3
  • 21
  • 21
  • Looking at the code, it should be a read only property because it is set using the internal state of the `Square` instance. What do you think? – shahkalpesh May 02 '11 at 07:16
  • This is just a silly example I made to show what I am thinking of. Depending on the purpose of the class, I would say you could be correct, but I would most likely not create a class to have a non mutable square :) – ostler.c May 02 '11 at 07:18
  • I suggest the use of read only property for `Vector` and not `Square`. – shahkalpesh May 02 '11 at 07:20
  • Would placing Pos = new Vector2(Width, Height) * Area in Square's constructor be a more maintainable approach? Or maybe an overloaded constructor? – bricklayer137 May 02 '11 at 07:43
  • @bricklayer137 - Thanks for the suggestion, unfortunately that is not what I want – ostler.c May 02 '11 at 16:42

1 Answers1

1

You can't do this as written, but you can define the Pos property to do the same thing. Instead of

public Vector2 Pos { get; set; }

do this

public Vector2 Pos
{
    get 
    {
        return new Vector2(Width, Height) * Area;
    }
}

Of course, then any square has the same definition for Pos. Not sure if that's what you want.

Edit

Based on your comment I take it you want to be able to specify the value of Pos deferently for different Squares. Here's another idea. You could add a third argument to the constructor which takes a delegate, and then the constructor could use the delegate internally to set the property. Then when you create a new square you just pass in a lambda for the expression you want. Something like this:

public Square(int width, int height, Func<Square, Vector2> pos) 
{ 
    Width = width; 
    Height = height; 
    Pos = pos(this);
}

then

Square mySquare = new Square(4, 5, sq => new Vector2(sq.Width, sq.Height) * sq.Area);
Andrew Cooper
  • 32,176
  • 5
  • 81
  • 116
  • Thanks, unfortunately that is not what I want, but you are you are correct, this is not possible according to [this](http://stackoverflow.com/questions/3564626/c-object-initialiser-reference-to-the-new-instance) – ostler.c May 02 '11 at 07:26