0

For some reason, I'm getting build errors when trying to use an expression body for the constructor if a viewmodel.

This works:

public class SolutionMaintenanceViewModel
{
    public SolutionMaintenanceViewModel(IActiveUserService currUserSrv)
    {
        _currUserSrv = currUserSrv;
    }
    ...
}

Whereas this:

public class SolutionMaintenanceViewModel
{
    public SolutionMaintenanceViewModel(IActiveUserService currUserSrv) => _currUserSrv = currUserSrv;
    ...
}

Gives the following:

1>C:\MyPath\Models\SolutionMaintenanceViewModel.cs(16,77,16,79): error CS1002: ; expected

1>C:\MyPath\Models\SolutionMaintenanceViewModel.cs(16,77,16,79): error CS1519: Invalid token '=>' in class, struct, or interface member declaration

1>C:\MyPath\Models\SolutionMaintenanceViewModel.cs(16,93,16,94): error CS1519: Invalid token '=' in class, struct, or interface member declaration

1>C:\MyPath\Models\SolutionMaintenanceViewModel.cs(16,106,16,107): error CS1519: Invalid token ';' in class, struct, or interface member declaration ========== Build: 0 succeeded, 1 failed, 4 up-to-date, 0 skipped ==========

Worth noting, no errors show up under the Error List window in Visual Studio. Only under the Output window.

Why is this happening?

Using .NET Framework 4.7.2

Edit: on request, here's the entirety of the viewmodel:

public class SolutionMaintenanceViewModel
{
    private IActiveUserService CurrUserSrv;

    public SolutionMaintenanceViewModel(IActiveUserService currUserSrv) => CurrUserSrv = currUserSrv;

    public List<ConcernArea> AllConcernAreas { get; set; }

    public IEnumerable<DealerFeedback> AllDealerFeedback { get; set; }

    public List<PortalProductSubGroup> AllPPSGs { get; set; }

    public string AttachmentGUID { get; set; }

    public string AttachmentGUIDs { get; set; }

    public List<SolutionAttachment> Attachments { get; set; }

    public bool CanSaveSubmit =>
        (Status == SolutionStatus.NewDraft || Status == SolutionStatus.MaintenanceDraft)
        && IsDealerOrHasMaintenancePermission;

    private bool IsDealerOrHasMaintenancePermission =>
        CurrUserSrv.GetActiveUser() is IDealer || CurrUserSrv.GetActiveUser() is InternalUser &&
        ((InternalUser)CurrUserSrv.GetActiveUser()).GetPermission(PermissionsProgramAreas.MAINTAINSOLUTIONS
            .ToString()) == PermissionsValue.WRITE;

    public bool CanApprove =>
        Status == SolutionStatus.Pending && CurrUserSrv.GetActiveUser() is InternalUser &&
        ((InternalUser)CurrUserSrv.GetActiveUser()).GetPermission(PermissionsProgramAreas.APPROVESOLUTIONS
            .ToString()) == PermissionsValue.WRITE;

    public bool CanDelete =>
        DealerDraftOrAuthorizedOrLastMaintained
        && HasBeenSaved()
        && (NewDraftCanBeDeleted || !HasEverBeenApproved);

    private bool DealerDraftOrAuthorizedOrLastMaintained =>
        !string.IsNullOrEmpty(OriginallyCreatedByDealerUsername) || CurrUserSrv.GetActiveUser() is InternalUser &&
        (((InternalUser)CurrUserSrv.GetActiveUser()).GetPermission(PermissionsProgramAreas.DELETESOLUTIONS
             .ToString()) == PermissionsValue.WRITE || CurrUserSrv.GetActiveUser().Username == UserModified);

    private bool NewDraftCanBeDeleted =>
        Status == SolutionStatus.NewDraft
        && (
            string.IsNullOrEmpty(OriginallyCreatedByDealerUsername)
            || CurrUserSrv.GetActiveUser() is IDealer
            && CurrUserSrv.GetActiveUser().Username == OriginallyCreatedByDealerUsername
        );

    private bool HasBeenSaved() => VersionID != 0;

    public bool CanDisapprove =>
        (Status == SolutionStatus.Pending || Status == SolutionStatus.Approved)
        && CurrUserSrv.GetActiveUser() is InternalUser
        && ((InternalUser)CurrUserSrv.GetActiveUser()).GetPermission(PermissionsProgramAreas.APPROVESOLUTIONS
            .ToString()) == PermissionsValue.WRITE;

    public bool CanSeeAttachments =>
        CurrUserSrv.GetActiveUser() is InternalUser || Status == SolutionStatus.Approved;

    public bool CanAddAttachments =>
        CurrUserSrv.GetActiveUser() is InternalUser
        && (Status == SolutionStatus.MaintenanceDraft || Status == SolutionStatus.NewDraft);

    [MaxLength(2000)]
    public string Cause { get; set; }

    [Required]
    [MaxLength(2000)]
    public string Complaint { get; set; }

    [Required]
    public int CompanyNumber { get; set; }

    [Required]
    public int ConcernAreaID { get; set; }

    [Required]
    [MaxLength(2000)]
    public string Correction { get; set; }

    [MaxLength(2000)]
    public string DealerCreatedNotes { get; set; }

    public DealerFeedback DealerFeedback { get; set; }

    [MaxLength(2000)]
    public string DealerNotes { get; set; }

    public bool HasEverBeenApproved { get; set; }

    public int HeaderID { get; set; }

    [MaxLength(2000)]
    public string InternalNotes { get; set; }

    public IEnumerable<string> ItemNums { get; set; }

    public bool IsSubmit { get; set; }

    public string Key { get; set; }

    public string OriginallyCreatedByDealerUsername { get; set; }

    public int? PortalProductSubGroupID { get; set; }
    public IEnumerable<string> SolutionSerialsDistinctSerialNums { get; set; }
    public int? SolutionSerialsSerialRangeFrom { get; set; }
    public int? SolutionSerialsSerialRangeTo { get; set; }

    public SolutionStatus Status { get; set; }

    [Required]
    [MaxLength(100)]
    public string Title { get; set; }

    public string UserModified { get; set; }

    public int VersionID { get; set; }
}
Sarov
  • 545
  • 6
  • 17
  • What version of .Net/C# are you using? – Hayden Sep 17 '19 at 15:16
  • @Hayden .NET 4.7.2 – Sarov Sep 17 '19 at 15:18
  • This is not an issue with the expression body, but most likely an error somewhere else in the class. Notice the error message has a column (77). The total length of that constructor is greater than 77. If we're able to see more of the ViewModel, it will help. – Hayden Sep 17 '19 at 15:26
  • @Hayden dumped the whole thing. – Sarov Sep 17 '19 at 15:42
  • What version of c# are you using? c#6 doesn't allow expression bodied constructors - so make sure you're compiling on c# 7 or higher. – Zohar Peled Jul 02 '20 at 05:21
  • @ZoharPeled expression body ctors worked in other files, so pretty sure C# 7. Also, by now the code has changed and there's no ctor at all anymore, so I'm no longer than interested in this Question. – Sarov Jul 03 '20 at 15:13

0 Answers0