开发者

Custom getters/setters in .NET - MVC

[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 Potentia开发者_开发技巧lFutureExposureOverride 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.


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


Are you sure you don't want your getter as

get { return PotentialFutureExposureOverride ?? STPData.MaximumCreditExposure; }

?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜