我有一個包含在operationComboBox.Text中的字符串,我知道該字符串將是「+」或「 - 」。然後我就可以使用此代碼執行2個方程之間的加減運算:我可以使用字符串值在C#計算中替換+或 - 運算符嗎?
if ((operationComboBox.Text == "-"))
{
equation3XCoeff = equations[index1].XCoeff - equations[index2].XCoeff;
equation3YCoeff = equations[index1].YCoeff - equations[index2].YCoeff;
equation3Answer = equations[index1].Answer - equations[index2].Answer;
}
else //if (operationComboBox.Text=="+")
{
equation3XCoeff = equations[index1].XCoeff + equations[index2].XCoeff;
equation3YCoeff = equations[index1].YCoeff + equations[index2].YCoeff;
equation3Answer = equations[index1].Answer + equations[index2].Answer;
}
我的問題是,我可以擺脫if語句,並直接在資金使用字符串值來進行,以縮短我的代碼如何?它可能不是太重要,但我只是想我的代碼很短,三個計算幾乎是重複的,但對於標誌。
可能重複:http://stackoverflow.com/questions/13522693/c-sharp-convert-string-to-operator –