1

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!

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I suspect the problem is that ChangeArray is changing the value of laoArrOriginal, which alters the "outer" array without mutation the previous value of the "inner" array object. It's very unclear at the moment though...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.