C#C
C#11mo ago
DaClownie

Accessing the value contained within a record struct

I have a record struct of AssessmentResponse:
public record struct AssessmentResponse
    (
        Guid Id,
        string Name,
        int Type,
        DateTime DateStart,
        DateTime DateEnd
    );

I understand this is an immutable object, in that once its been created it can't be modified... but how do I get access to the Id of this DTO for use in my code?
[Parameter]
public AssessmentResponse? ParentAssessment { get; set; }

Guid assessmentId = ParentAssessment.Id;

obviously the last line doesn't work but I'm not trying to modify the record struct, I just want access to the data.
I'm passing the AssessmentResponse? if it exists to the Blazor component, and want to access the Id section for the API call to update the record in my database. My AssessmentService.Update call requires an Guid, AssessmentRequest

I guess alternatively I could modify the AssessmentRequest to have an Id property contained within it and allow it to be nullable? but there's got to be a way to access the Id within the record struct and assign its value to another variable for use elsewhere.
Was this page helpful?