Performing an operation based on values within an array

Posted by James W. on Stack Overflow See other posts from Stack Overflow or by James W.
Published on 2013-10-21T03:38:36Z Indexed on 2013/10/21 3:53 UTC
Read the original article Hit count: 92

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about arrays