Diin
Diin

Reputation: 585

Conditional property in C# class

I have this code that calculate dues based on Income value at a specified rate. as below:

public decimal Income { get; set; }
public decimal Rate { get; set; }
public string Dues
{
    get 
    {
        return string.Format("{0}", Math.Round((this.Income * (1 /this.Rate) * 0.01m), 2));
    }
}

What I want to do is check if the Dues calculation is less than 5.00 then it should set the Dues value to 5.00. I am not sure if this can be done.

Any help will be appreciated.

Upvotes: 1

Views: 3035

Answers (1)

TanvirArjel
TanvirArjel

Reputation: 32099

Write as follows:

public class Foo
{
    public decimal Income { get; set; }
    public decimal Rate { get; set; }
    public decimal Dues
    {
        get
        {
            decimal totalDues = Math.Round((this.Income * (1 / this.Rate) * 0.01m), 2);
            return totalDues >= 5.00M ? totalDues : 5.00M;
        }
    }
}

Upvotes: 3

Related Questions