-4

I want to know how to add a value to an element of an array. This is not a duplicate as i don't want to add a new elemnt or assign an entirely new value but want to edit an existing value in the array. I'd imagine it would look something like this:

textArray[i] = currentElement + variable;

The example above does for some reason not work though

Thanks in advance! :)

HappyHippo
  • 152
  • 3
  • 11

2 Answers2

0

You can use textArray[i] += variable; or the more verbose textArray[i] = textArray[i] + variable;. In the second version note that the right hand side of = is fully evaluated before the assignment is made to textArray[i].

In this sense textArray[i] can be used both to "get" and "set" a value of an element in the array.

Just make sure that i is a valid array index in advance.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

You can use the SetValue method as below. textArray.SetValue(1,1); where first parameter is the element and second element is the index.

Subbu
  • 1
  • 1
  • but I want to add to an existing element not replace it with another value. For example: if the value of the array at index 1 is 2, I'd want to add 3 to it so that the value of the element is now 5 – HappyHippo Nov 11 '16 at 12:32