Melvin Smith
Melvin Smith

Reputation: 61

Directly change an array contained in an array

I have a about 20 string arrays on which I want to perform the same operation (change specific entries to another value). Therefore I have already written a method:

public static void ChangeArray<T>(ref T[,] laoArrOriginal, String lvsToChange, String lvsChangeValue)
{
        int dimRow = laoArrOriginal.GetLength(0);
        int dimCol = laoArrOriginal.GetLength(1);
        for (int i = 0; i < dimRow; i++)
        {
            for (int j = 0; j < dimCol; j++)
            {
                if (laoArrOriginal[i, j] == lvsToChange)
                {
                    laoArrOriginal[i, j] = lvsChangeValue;
                }
            }
        }       
}

Instead of calling 20 times this function with another array name, I thought about creating an array lcsStringArrays of my 20 arrays

String[][,] lcsStringArrays = new String[][,]{array1,array2,...}

and change them in a for loop:

for (int i = 0; i < lcsStringArrays.Length; i++ )
  {
       ChangeArray(ref lcsStringArrays[i], l_dblRecordCount, 1);
  }

But after looping the single elements array1, array2, etc. are unaltered while the element lcsStringArrays[i] has the right content.

What I am doing wrong?

EDIT: I solved this "problem". My code in ChangeArray was wrong. I inserted the code I use now; for the case someone comes here to search for a similar solution. Thank you anyways!

Upvotes: 1

Views: 69

Answers (1)

Charles Prakash Dasari
Charles Prakash Dasari

Reputation: 5122

Looks like we need the implementation of ChangeArray method as well. At the high level, I think you are changing the value of the variable (array) passed in. Why would you need this to be ref anyways? You are not changing the value of the array itself, you are changing the contents that are held by the array. You don't need ref for that purpose.

Upvotes: 1

Related Questions