gpt4 book ai didi

c# - 无法使用枚举隐式转换类型

转载 作者:太空宇宙 更新时间:2023-11-03 17:41:42 26 4
gpt4 key购买 nike

试图解决这个问题。即使它们实际上是相同的,似乎也无法将一个 num 分配给另一个。

Cannot implicitly convert type 'Project.Models.Concern.YesNo' to 'Project.ViewModels.PatientConcernsViewModel.YesNo'. An explicit conversion exists (are you missing a cast?)


public class Concern
{
public int ConcernID { get; set; }
public YesNo LifeExpectancy { get; set; }
public YesNo CollateralHistoryAvailable { get; set; }

public enum YesNo
{
Yes,
No
}
}

public class PatientConcernsViewModel
{
public int ConcernID { get; set; }

public YesNo LifeExpectancy { get; set; }
public YesNo CollateralHistoryAvailable { get; set; }

public enum YesNo
{
Yes,
No
}
}

// FROM WITHIN MY CONTROLLER

var concern = concernRepository.Find(id);

// load related assessment data
var assessment = assessmentRepository.Find(AssessmentID);

PatientConcernsViewModel patientConcernsViewModel = new PatientConcernsViewModel();

// BOTH LINES BELOW GENERATE ERRORS
patientConcernsViewModel.CollateralHistoryAvailable = concern.CollateralHistoryAvailable;
patientConcernsViewModel.LifeExpectancy = concern.LifeExpectancy;

最佳答案

让您的enum没有类的公共(public) - 然后他们可以在没有多个定义的情况下共享它。

public enum YesNo
{
Yes,
No
}

public class Concern
{
public int ConcernID { get; set; }
public YesNo LifeExpectancy { get; set; }
public YesNo CollateralHistoryAvailable { get; set; }
}

public class PatientConcernsViewModel
{
public int ConcernID { get; set; }

public YesNo LifeExpectancy { get; set; }
public YesNo CollateralHistoryAvailable { get; set; }
}

关于c# - 无法使用枚举隐式转换类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19275156/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com