1

I'm trying to figure out how to do operations based on values in an array. The values are taken from a string and inserted into the array

e.g

num = TextBox.Text.Split(' ');
results = Convert.ToDouble(num[0]);

for (int i = 0; i < num.Length - 1; i++)
   {
            if (num[i] == "+")
            {
                results += Convert.ToDouble(num[i++]);
            }
            ...
   }

So based on this, let's say the TextBox string value was "1 + 2". So the array would be:

-------------
| 1 | + | 2 |
-------------
  0   1   2 (indexes)

The part I'm having trouble with is Convert.ToDouble(num[i++]).. I've tried num[1] + 1, num[i + 1], etc I'm trying to figure out how to get it to perform the operation based on the first value and the value in the index after the operator. Which is the correct way to do something like this?

1 Answer 1

2

Try using ++i - the prefix increment :)

As James found this question explains the difference between postfix and prefix :)

Sign up to request clarification or add additional context in comments.

2 Comments

oh wow thanks haha I spent more time on this than I needed to. For future reference: stackoverflow.com/questions/24853/…
Thanks for that link, I was going to make my own testcase but visual studio was taking a while to start up :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.