13

The array I'm using is int[,,] and I want to make the first value of each (what I assume to be) tuple in one method and then I want to modify the second two values several times. (in another method)

For example:

int[,,] MyArrayGet()
{    
    int [,,] myArray;
    int [,,] myArray = new int[9,9,9];
    for (int i = 0; i < 10; i++)
    {
        myArray[i] = [SomeInt,0,0];
    }
    return myArray;
}
int[,,] MyArrayModify(int[,,] myArray)
{   
    for (int i = 0; i < 10; i++)
    {
        if (somthing is true) 
        {
            myArray[i] = [dont change this value,n+1,dont change this value]
        }
        if (somthingelse is true)
        {
            my  Array[i] = [dont change this value,dont change this value,n+1]
    }
}

Is there a quick and easy way to do this?

I have checked this question How to get array of string from List<Tuple<int, int, string>>? however either I do not feel it answers my question.

Community
  • 1
  • 1
bubblez go pop
  • 147
  • 1
  • 1
  • 10
  • See http://stackoverflow.com/questions/3131400/why-tuples-items-are-readonly – Tarik Jul 21 '16 at 07:53
  • being a beginner, i do not understand the answers to the question you suggested, my apologize. @Tarik – bubblez go pop Jul 21 '16 at 08:07
  • I pointed you to an answer explaining that you cannot modify a tuple. I then provided an answer below that should do the job. Please, feel free to ask further for explanations. Please, post some code on what you are trying to achieve. – Tarik Jul 21 '16 at 08:33
  • What you are creating is not a tuple. It is a 3-dimensional array, consisting of 9*9*9 = 729 integer values. Are you expecting to have 9 tuples of 3 ints each? Then answer from @tommy is what you are looking for. Or create new int[9,3] - 9 rows, 3 columns each. BTW your for loop is out of bounds. Should be i < 9 – Alexander Jul 21 '16 at 09:10

2 Answers2

21

Tuple by design is immutable. Just create new one when modifying.

IEnumerable<Tuple<int, int, int>> Get()
{
    for (int i = 0; i < 10; i++)
    {
        yield return Tuple.Create(i, 0, 0);
    }
}

IEnumerable<Tuple<int, int, int>> Modify(IEnumerable<Tuple<int, int, int>> tuples)
{
    foreach (var tuple in tuples)
    {
        if (tuple.Item1 < 5)
        {
            yield return Tuple.Create(tuple.Item1, tuple.Item2 + 1, tuple.Item3);
        }
        else
        {
            yield return Tuple.Create(tuple.Item1, tuple.Item2, tuple.Item3 + 1);
        }
    }
}

Your code looks like javascript more than C#. int[,,] is 3D array not array of 3 items.

Tuple<int, int, int>[] is an array of Tuple of 3 items. If you are not a beginner and use LINQ, IEnumerable<Tuple<int, int, int>> is a better version.

Tommy
  • 3,044
  • 1
  • 14
  • 13
6

You have to replace the tuple you want to modify by a new tuple containing the updated value. Alternatively, you can create a class with the fields that you need and public properties. That will only take seconds and probably make your code more readable.

Tarik
  • 10,810
  • 2
  • 26
  • 40
  • 1
    Agreed, once you are starting to want to modify values, just create a class instead of creating more code to solve a simple issue. – AndrewK Apr 27 '18 at 14:53