double positions = Convert.ToDouble(textBox2.Text);
if (textBox3.Text == "Long")
{
positions = positions;
}if (textBox3.Text == "Short")
{
positions = positions * -1;
}
Is there any other way I can use a condition where if the textbox3 says "Long" it will convert position to and if it says "Short" converts the position number to negative. I don't want to use if statement because it creates two different blocks of code. I just want one block.
For example: if textbox3 is Long or short: convert position to negative or positive, then I'm able to use position in a method below.
CodePudding user response:
How about an inline if statement ?
positions = textBox3.Text == "Long" ? positions : positions * -1;
This will only work if there are only two possible values for textBox3.Text
CodePudding user response:
you can initiate positions with inline if statement :
double positions = textBox3.Text == "short" ?
Convert.ToDouble(textBox2.Text) * (-1) :
Convert.ToDouble(textBox2.Text);
if you want do something else when textBox3.Text == "Long" :
double positions = textBox3.Text == "short" ?
Convert.ToDouble(textBox2.Text) * (-1) :
textBox3.Text == "Long" ?
// do something on Convert.ToDouble(textBox2.Text) :
Convert.ToDouble(textBox2.Text);
CodePudding user response:
You can negate the position
. This way you only need 1 operation.
if (textBox3.Text == "Long" || textBox3.Text == "Short") { positions = positions * -1; }
Ideally you want to do this in a method and pass a string parameter.