Why cannot initialize an object with object initializer syntax?

Shervan360 1,481 Reputation points
2022-05-28T13:56:57.317+00:00

Hello,

Could you please help me?

namespace cSharp10  
{  
    public class Position  
    {  
        public int X { get; set; }  
        public int Y { get; set; }  
    }  
    public class Size  
    {  
        public int Width { get; set; }  
        public int Height { get; set; }  
    }  
    public class Shape  
    {  
        public Position Position { get; } = new Position();  
        public Size size { get; } = new Size();  
        public void Draw() => DisplayShape();  
        protected virtual void DisplayShape()  
        {  
            Console.WriteLine($"Shape With {Position} and {size}");  
        }  
        public virtual Shape Clone() => throw new NotImplementedException();  
    }  
    public class Rectangle : Shape  
    {  
        protected override void DisplayShape()  
        {  
            Console.WriteLine($"Rectangle at position {Position} with size {size}");  
        }  
        public override Rectangle Clone()  
        {  
  
            Rectangle r = new() // I have an error... here..  
            {  
                Position.X = 10,  
                Position.Y = 20,  
                size.Width = 1,  
                size.Height = 2  
            };  
            return r;  
        }  
    }  
    internal class Program  
    {  
        static void Main(string[] args)  
        {  
        }  
  
    }  
}  

206325-screenshot-2022-05-28-095416.png

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,099 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 110.8K Reputation points
    2022-05-28T14:31:45.727+00:00

    Try:

    Rectangle r = new( )
                {
                    X = 10,
                    Y = 20,
                    Width = 1,
                    Height = 2
                };
    

  2. Peter P 6 Reputation points
    2022-05-28T14:57:46.71+00:00

    You are trying to set child objects which isn't allowed.
    Instead of setting the values of the child objects, why not pass though objects to use as prototypes to set the values from?

        namespace cSharp10 {
            public class Position {
                public int X { get; set; }
                public int Y { get; set; }
                public void SetFrom(Position position) => (X, Y) = (position.X, position.Y);
            }
            public class Size {
                public int Width { get; set; }
                public int Height { get; set; }
                public void SetFrom(Size size) => (Width, Height) = (size.Width, size.Height);
            }
            public class Shape {
                private Position _position { get; } = new ();
                public Position Position { get => _position; set => _position.SetFrom(value); }
                private Size _size { get; } = new Size();
                public Size size { get =>_size; set => (_size.Width, _size.Height) = (value.Width, value.Height); } 
                public void Draw() => DisplayShape();
                protected virtual void DisplayShape() {
                    Console.WriteLine($"Shape With {_position} and {size}");
                }
                public virtual Shape Clone() => throw new NotImplementedException();
            }
            public class Rectangle : Shape {
    
                protected override void DisplayShape() {
                    Console.WriteLine($"Rectangle at position {Position} with size {size}");
                }
                public override Rectangle Clone() {
    
                    Rectangle r = new () // I have an error... here..
                    {
                        Position = new Position() { X = 10, Y = 20 },
                        size = new Size() { Width = 1, Height = 2 }
                    };
                    return r;
                }
            }
            internal class Program {
                static void Main(string[] args) {
                }
    
            }
        }