Performing an operation based on values within an array
- by James W.
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?