简体   繁体   中英

Is there a way to shorten repetitive math

I have a math equation that is repeated for multiple variable but each time only the output and 1 variable is changed

R1S2NCtxt.Text = ((((ATT * R1S2) - EDEF) * 0.8M) + ((ATT * Pierce) - EDEF * ERES)).ToString();
R2S2NCtxt.Text = ((((ATT * R2S2) - EDEF) * 0.8M) + ((ATT * Pierce) - EDEF * ERES)).ToString();
R3S2NCtxt.Text = ((((ATT * R3S2) - EDEF) * 0.8M) + ((ATT * Pierce) - (EDEF * ERES))).ToString();

This is repeated multiple times is there a way to shorten this or repeat formula with different variables thx

You could use a function to get rid of copy pasting code.

Example:

private string Calculate(float number) {
    return  ((((ATT * number) - EDEF) * 0.8M) + ((ATT * Pierce) - EDEF * ERES)).ToString();
}

Now you can call that function with:

R1S2NCtxt.Text = Calculate(R1S2);
R2S2NCtxt.Text = Calculate(R2S2);
R3S2NCtxt.Text = Calculate(R3S2);
decimal Equation(decimal value)
    => (((ATT * value) - EDEF) * 0.8M) + ((ATT * Pierce) - EDEF * ERES);

Then:

R1S2NCtxt.Text = Equation(R1S2).ToString();

Assuming R1S2 is decimal ; if not then just amend the type of value accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM