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".