属性或索引器不能被分配​​到 - 它是只读 [英] Property or indexer cannot be assigned to -- it is read only

查看:248
本文介绍了属性或索引器不能被分配​​到 - 它是只读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我捕捉出生和诊断日期的日期在数据库中,并希望通过诊断日期来计算岁。

这是我的code。但我有错误下面的错误。

  

错误2属性或索引   LightSwitchApplication.Patient.AgeAtDiagnosis'不能被分配​​到    - 它是只读

 部分无效AgeAtDiagnosis_Compute(REF INT结果)
{
        //设置结果所需的字段值
        AgeAtDiagnosis = DateofDiagnosis.Year  -  DateofBirth.Year;
        如果(出生日期> DateofDiagnosis.AddYears(-AgeAtDiagnosis))
        {
           AgeAtDiagnosis--;
        }
}
 

解决方案

  DateTime的诞生=新的日期时间(1950,01,01);
DateTime的诊断=新的日期时间(2012,02,01);
时间跨度跨度=诊断 - 出生;
日期时间年龄= DateTime.MinValue +跨度;
//注意:MINVALUE是1/1/1,所以我们必须减去...
INT年= Age.Year  -  1;
 

I'm capturing date of Birth and Date of Diagnosis in database and would like to calculate age in years by Diagnosis date.

This is my code. But I have error following error.

Error 2 Property or indexer 'LightSwitchApplication.Patient.AgeAtDiagnosis' cannot be assigned to -- it is read only

partial void AgeAtDiagnosis_Compute(ref int result)
{
        // Set result to the desired field value
        AgeAtDiagnosis = DateofDiagnosis.Year - DateofBirth.Year;
        if (DateofBirth > DateofDiagnosis.AddYears(-AgeAtDiagnosis))
        {
           AgeAtDiagnosis--;
        }
}

解决方案

DateTime birth = new DateTime(1950, 01, 01);
DateTime diagnosis = new DateTime(2012, 02, 01);
TimeSpan Span = diagnosis - birth;
DateTime Age = DateTime.MinValue + Span;
// note: MinValue is 1/1/1 so we have to subtract...
int Years = Age.Year - 1;

这篇关于属性或索引器不能被分配​​到 - 它是只读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆