赋值运算符不起作用

时间:2016-05-04 19:34:08

标签: c#

当我调试代码时,我看到有一个数值,但它没有更新Model.Weight

private Person Model { get; set; }

public void Init(string pId)
{

//只是从本地数据库获取数据,它不是空的       Model = _personService.GetPerson(pId);     }

public decimal Weight
{
   get { return Model.Weight; }
   set
   {
     //there is a numeric value here, but Model.Weight does not get updated.
     Model.Weight = value;
   }
}

Person.cs

public class Person
{
    public decimal Weight { get; set; }
}

1 个答案:

答案 0 :(得分:2)

作业正常:

using System;

class Foo
{
    static void Main()
    {
        var foo = new Foo();
        foo.Init("abc");
        var person = foo.Model;
        foo.Weight = 123.45M;
        Console.WriteLine(person.Weight); // 123.45
    }
    private Person Model { get; set; }

    public void Init(string pId)
    {
        Model = _personService.GetPerson(pId);
    }

    public decimal Weight
    {
        get { return Model.Weight; }
        set { Model.Weight = value; }
    }
}
public class Person
{
    public decimal Weight { get; set; }
}
static class _personService // yes I know this isn't representative of your setup
{
    internal static Person GetPerson(string pId)
    {
        return new Person();
    }
}