I have a few local variables and I want to divide them all divide them all by the same number.
decimal a = 0;
decimal b = 0;
decimal c = 0;
...
decimal n = 0;
decimal divisor = 0;
<perform calculations to give all variables meaningful values>
divide each decimal (a - n) by divisor then assign value
Beside dividing and assigning every variable with:
a = a / divisor;
b = b / divisor;
and so on...
Is there a faster way? I’m thinking something along the lines of putting them all in a collection and iterating over it…
I don’t need the values in a list, I need the variables to contain them. I was thinking of something along the lines using a list of pointers, iterating over it and setting the values that way.
“Be aware that this isn’t the same as the first code though – it makes the list variable refer to a new list containing the divided numbers; if anything else has a reference to the original list, it won’t see any changes.” – J Skeet
Per the question – “I don’t need the values in a list, I need the variables to contain them. I was thinking of something along the lines using a list of pointers, iterating over it and setting the values that way.”
This is not possible and as Mr. Copsey pointed out – “You’re better off working with the list of values, as Jon suggested. Trying to make a list of pointers to the original values, iterate over them, etc, will be more work than just setting them directly (plus much less maintainable).”