1

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

无法将类型“Project.Models.Concern.YesNo”隐式转换为“Project.ViewModels.PatientConcernsViewModel.YesNo”。存在显式转换(您是否缺少演员表?)

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;
4

4 回答 4

5

在没有课程的情况下公开您enum的课程 - 然后他们可以在没有多个定义的情况下共享它。

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; }
}
于 2013-10-09T14:44:06.317 回答
5

摆脱您的YesNo枚举并将其替换为System.Boolean. 尽可能使用系统类型。

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

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

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

}
于 2013-10-09T14:45:06.990 回答
0

为什么不YesNo限定枚举以便两者都访问同一个对象?

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; }

}
于 2013-10-09T14:45:08.867 回答
0

您有 2 个完全不同的枚举,它们恰好在完全限定名称中具有相同的项目和相同的最后一个单词。

正如错误所述,您拥有Project.Models.Concern.YesNoProject.ViewModels.PatientConcernsViewModel.YesNo

如果您只是将枚举放在其他更合适的地方,那么两个类都可以使用它并且您没有两种类型。

虽然上面回答了您的问题,因为 Aron 指出,对于我能想象的几乎所有目的,bool 会比 Yes/No 枚举更好。

于 2013-10-09T14:46:50.500 回答