我有這樣的代碼:維護對結構或其他重構此代碼的方式的引用?
BigInteger lhs = new BigInteger(0);
BigInteger rhs = new BigInteger(0);
bool isLeftHandSide = true;
for (int i = 0; i < txtResult.Text.Length; i++)
{
char currentCharacter = txtResult.Text[i];
if (char.IsNumber(currentCharacter))
{
char currentCharacter = txtResult.Text[i];
if (isLeftHandSide)
{
lhs += (int)char.GetNumericValue(currentCharacter);
}
else
{
rhs += (int)char.GetNumericValue(currentCharacter);
}
}
}
我在想,如果有辦法,而不是存儲到當前方的參考,這樣我可以操作這些變量,而不是和擺脫if和else語句的。我希望能夠做到這一點,而不使用不安全的代碼上下文。
喜歡的東西替換這樣的:
if (isLeftHandSide)
{
lhs += (int)char.GetNumericValue(currentCharacter);
}
else
{
rhs += (int)char.GetNumericValue(currentCharacter);
}
利用該:
currentSide += (int)char.GetNumericValue(currentCharacter);
哪裏currentSide是要麼LHS或RHS的參考。因爲它們是結構體,所以我不確定如何保留對它的引用而不是僅僅複製數據。
謝謝。
你必須添加上下文的方法和循環。 –
你可以在代碼片段中加入更多的上下文嗎? – Bobson
增加了一些更多的上下文。 –