.NET - MVC 中的自定义 getter/setter

发布于 2024-11-14 11:53:27 字数 683 浏览 2 评论 0原文

[DisplayFormat(DataFormatString = "{0:c}")]
[DataType(DataType.Currency)]
public decimal? PotentialFutureExposure
{
    get { return STPData.MaximumCreditExposure; }
    set 
    { 
        STPData.MaximumCreditExposure = value;
        this.PotentialFutureExposureOverride = value;
    }
}

public decimal? PotentialFutureExposureOverride { get; set; }

所以我有这两个属性。基本上,我希望 PotentialFutureExposureOverride 属性返回它自己的值(如果有),否则我希望它返回 PotentialFutureExposure 的值。现在,PotentialFutureExposure 在设置时设置覆盖,但由于目前的某些原因,PotentialFutureExposureOverride 仍然返回空白,即使 PotentialFutureExposure 有一个值。

谢谢。

[DisplayFormat(DataFormatString = "{0:c}")]
[DataType(DataType.Currency)]
public decimal? PotentialFutureExposure
{
    get { return STPData.MaximumCreditExposure; }
    set 
    { 
        STPData.MaximumCreditExposure = value;
        this.PotentialFutureExposureOverride = value;
    }
}

public decimal? PotentialFutureExposureOverride { get; set; }

So I have these two properties. Basically, I want the PotentialFutureExposureOverride property to return it's own value if it has one, otherwise I want it to return the value of PotentialFutureExposure. Now PotentialFutureExposure sets the override when it's set, but for some reason currently, PotentialFutureExposureOverride is still returning blank even though PotentialFutureExposure has a value.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何处潇湘 2024-11-21 11:53:27

这有效吗?

private decimal? _override = null;

public decimal? PotentialFutureExposure
{
    get { return PotentialFutureExposureOverride ?? STPData.MaximumCreditExposure; }
    set
    {
        STPData.MaximumCreditExposure = value;
        this.PotentialFutureExposureOverride = value;
    }
}

public decimal? PotentialFutureExposureOverride 
{ 
    get 
    {
        if (_override.HasValue)
            return _override;
        return PotentialFutureExposure;
    }
    set
    {
        _override = value;
    }
}

Does this work?

private decimal? _override = null;

public decimal? PotentialFutureExposure
{
    get { return PotentialFutureExposureOverride ?? STPData.MaximumCreditExposure; }
    set
    {
        STPData.MaximumCreditExposure = value;
        this.PotentialFutureExposureOverride = value;
    }
}

public decimal? PotentialFutureExposureOverride 
{ 
    get 
    {
        if (_override.HasValue)
            return _override;
        return PotentialFutureExposure;
    }
    set
    {
        _override = value;
    }
}
永不分离 2024-11-21 11:53:27

你确定你不想要你的 getter as 吗

get { return PotentialFutureExposureOverride ?? STPData.MaximumCreditExposure; }

Are you sure you don't want your getter as

get { return PotentialFutureExposureOverride ?? STPData.MaximumCreditExposure; }

?

梦里的微风 2024-11-21 11:53:27

PotentialFutureExposure 并没有真正的价值。在您的代码中,STPData.MaximumCreditExposure 具有实际值。在 PotentialFutureExposure setter 中设置之前,STPData.MaximumCreditExposure 是否可能已有值?如果是这种情况,那么 PotentialFutureExposure 看起来总是有一个值。

PotentialFutureExposure doesn't really have a value. In your code, STPData.MaximumCreditExposure has the actual value. Is it possible that STPData.MaximumCreditExposure has a value before you set it in the PotentialFutureExposure setter? If that is the case, then PotentialFutureExposure will always look as if it has a value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文