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 Answers1

2

Try using ++i - the prefix increment :)

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

Community
  • 1
  • 1
NPSF3000
  • 2,421
  • 15
  • 20
  • 1
    oh wow thanks haha I spent more time on this than I needed to. For future reference: http://stackoverflow.com/questions/24853/what-is-the-difference-between-i-and-i –  Oct 21 '13 at 03:46
  • 1
    Thanks for that link, I was going to make my own testcase but visual studio was taking a while to start up :) – NPSF3000 Oct 21 '13 at 03:47