I have this code:
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);
}
}
}
I was wondering if there was a way to instead store a reference to the current side so that I could just manipulate that variable instead and get rid of the if and else statements. I’d like to be able to do this without using unsafe code contexts.
Something like replacing this:
if (isLeftHandSide)
{
lhs += (int)char.GetNumericValue(currentCharacter);
}
else
{
rhs += (int)char.GetNumericValue(currentCharacter);
}
With this:
currentSide += (int)char.GetNumericValue(currentCharacter);
Where currentSide is a reference to either lhs or rhs. Because they are structs I’m not sure how to keep a reference to it rather than just copying the data.
Thanks.
You could capture the variable you want in a lambda, something like this:
and then to switch sides you just reassign
addToCurrentSidethus: