質問

In the following code ,I don't know the target of using internal status flag : (C#)

// internal status flag
    private int status = 0;

    // status constants
    private const int AMOUNT_SET = 1;
    private const int RATE_SET = 2;
    private const int PERIODS_SET = 4;
    private const int ALL_SET = AMOUNT_SET | RATE_SET | PERIODS_SET;
public double LoanAmount
        {
            get
            {
                return this.loanAmount;
            }
            set
            {
                this.loanAmount = Math.Abs(value); //taking the absolute value of the user input to ensure getting  positive values
                this.status |= AMOUNT_SET;
                CalcPayment(); //check if the method can calculate yet 
            }
        }

 public double InterestRate
        {
            get
            {
                return this.interestRate;
            }
            set
            {
                this.interestRate = Math.Abs(value);
                if (this.interestRate >= 1)
                    this.interestRate /= 100;    // if entered as a percent, convert to a decimal
                this.status |= RATE_SET;
                CalcPayment();
            }
        }

        public int Periods
        {
            get
            {
                return this.periods;
            }
            set
            {
                this.periods = Math.Max(Math.Abs(value), 1);
                this.status |= PERIODS_SET;
                CalcPayment();
            }
        }

        public double MonthlyPayment
        {
            get
            {
                return this.monthlyPayment;
            }
        }

        public override string ToString()
        {
            return "\n\tThis is a loan of " + this.loanAmount.ToString("c") + " at " + this.interestRate.ToString("p") +
                    " interest for " + this.periods.ToString() + " months,\n\t\twith a monthly payment of " +
                    this.monthlyPayment.ToString("c") + ".";
        }

        private void CalcPayment()   
        {                              
            if (this.status == ALL_SET)
            {
                double periodicInterestRate = interestRate / 12;
                double compound = Math.Pow((1 + periodicInterestRate), periods);
                this.monthlyPayment = this.loanAmount * periodicInterestRate * compound /                   (compound - 1);
            }
        }

So why did it use status flag ? Thanks

役に立ちましたか?

解決

It's just a clever way of making sure that all the required functions have been called.

Initially, the status flag as an int looks like this in binary:

0000  // truncated for clarity it would really be 32 0's

once the amount is set, the flag looks like this:

0001 

Once rate is set, it looks like

0011 // because 2 in binary is ...0010, so ORing 0001 and 0010 -> 0011

他のヒント

I would imagine the original intent was that the calculation was only done if all fields are set. The flag is important because double and int are both value types that have a default value of 0.0d and 0 respectively.

I can see this being done before C# had Nullable struct or the short cut T? where included in the language.

i would say that it is easier to use

 if (this.status == ALL_SET)

then having 3 nullable and doing (pseudo kind of code)

 if (this.LoanAmount.HasValue && this.InterestRate.HasValue && this.Periods.HasValue)

code preference i think

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top