Search code examples
c#.netrestrepository-pattern

Getting compile error when initializing classObject = new();


In our .NET REST-API, I have a DTO-class and a C# Controller-class. The DTO-class is as follows without any compilation errors. This DTO contains a variable for the INSPECTION and an IEnumerable/list-variable for the list of DEFECTS observed during the inspection. The 'list' may be empty when there are no defects. This is similar to ADDING the arrangement of an INVOICE having multiple INVOICE-ITEMS.

public class PTInspectionAndTMDefectsDTO {

public required PTINSPECTION PtInspection { get; set; }
public IEnumerable<TMDEFECT> TMDefects { get; set; } = Enumerable.Empty<TMDEFECT>();

// Constructors.
public PTInspectionAndTMDefectsDTO(PTINSPECTION ptinspection) { PtInspection = ptinspection; }
public PTInspectionAndTMDefectsDTO(PTINSPECTION ptinspection, IEnumerable<TMDEFECT> tmdefects) { PtInspection = ptinspection; TMDefects = tmdefects; }
}

Here is the controller-function code where I initialize PTInspectionAndTMDefectsDTO createdInspectionAndDefects using the first constructor providing the required PTInspection-variable. A compilation error appears in the last C#-sentence.

[HttpPost("AddPTInspectionAndDefects")]
public async Task<IActionResult> AddPTInspectionAndDefects([FromBody] PTInspectionAndTMDefectsDTO oDataToAdd) {
    PTINSPECTION addThisInspection = oDataToAdd.PtInspection;
    IEnumerable<TMDEFECT>? addTheseDefects = oDataToAdd.TMDefects;
    PTINSPECTION createdInspection = new PTINSPECTION();
    TMDEFECT? createdDefect = new TMDEFECT();
    IEnumerable<TMDEFECT> createdDefectList = Enumerable.Empty<TMDEFECT>();
    PTInspectionAndTMDefectsDTO createdInspectionAndDefects = new(createdInspection); 
... a compilation error appears for the above sentence with a red-squiggle under the word [new] -- see image below.
... other code is not provided...

enter image description here

The error CS9035 has no further information. I am not sure why the new(createdInspection); is not valid. Your assistance is welcome. Thanks...John


Solution

  • You need to mark the ctors with SetsRequiredMembersAttribute.

    Otherwise, you have to set the required properties in the object initializer.

    using System.Diagnostics.CodeAnalysis;
    
    public class PTInspectionAndTMDefectsDTO
    {
        public required PTINSPECTION PtInspection { get; set; }
    
        public IEnumerable<TMDEFECT> TMDefects { get; set; } = Enumerable.Empty<TMDEFECT>();
    
        // Constructors.
        [SetsRequiredMembers]
        public PTInspectionAndTMDefectsDTO(PTINSPECTION ptinspection)
        {
            PtInspection = ptinspection;
        }
    
        [SetsRequiredMembers]
        public PTInspectionAndTMDefectsDTO(PTINSPECTION ptinspection, IEnumerable<TMDEFECT> tmdefects)
        {
            PtInspection = ptinspection;
            TMDefects = tmdefects;
        }
    }
    

    Please note:

    Should you have a ctor that does not set all the required properties then do not mark that ctor with [SetsRequiredMembers].