question

Shervan360-6172 avatar image
0 Votes"
Shervan360-6172 asked PeterP-2997 commented

Why cannot initialize an object with object initializer syntax?

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


dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
0 Votes"
Viorel-1 answered PeterP-2997 commented

Try:

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

· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Unfortunately, it was not correct.

0 Votes 0 ·
Viorel-1 avatar image Viorel-1 Shervan360-6172 ·

What do you mean?


0 Votes 0 ·

I have an error with your snippet code.

0 Votes 0 ·
Show more comments
PeterP-2997 avatar image
0 Votes"
PeterP-2997 answered PeterP-2997 commented

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) {
             }
        
         }
     }
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Position pos = new() { X = 10, Y = 20};

gets lowered into

Position pos = new Position();
pos.X = 10;
pos.Y = 20;


With the first line the values being set are known before the object is created.
However for the lowered result they are set afterwards.
It wouldn't make sense to the compiler what order to initialize the child objects.

The compiler can't lower the child types as they are only created after the type has already been initialized.


https://github.com/dotnet/roslyn/blob/main/src/Compilers/CSharp/Portable/Lowering/InitializerRewriter.cs


1 Vote 1 ·