0

My goal is to rename an instantiated array.

Below is a test case. The actual program I have reads data from an excel spread sheet, performs filtering and type conversion, and then stores that data in an array (e.g. the allData array below).

Once the data is stored in the array I need to split the array into two copies of itself and perform a set of separate tasks on each of those copies.

To achieve this I created a clone (in the example arr1). Now I have two copies of the same data (allData and arr1) but the term allData, that was appropriately named in the past code, now feels inappropriately named, so I would like to rename it.

I considered creating another clone, but that would just leave the array (allData) sitting there unused and needlessly taking up memory.

I also considered arr2 = allData but that just leaves a reference (allData) sitting there needlessly. What I really want is to rename allData to arr2.

namespace nspace
{
    public class c
    {
        public static void Main()
        {

        object[][] allData = new object[10][];

        for (int i = 1; i <= 10; i++)
        {
                for (int j = 1; j <= 5; j++)
                {
                        // fill array with something
                }
        }

        arr1 = allData.clone();
        // Rename array allData to arr2
        }
    }
}
Phoenix
  • 4,386
  • 10
  • 40
  • 55
  • actually `arr2 = allData` this is OK, only the reference is copied, dont give yourself a headache it will save a ton of time – Anonymous Duck Nov 11 '16 at 09:32
  • Why not make it object[][][]allDataInstances = new object[2][][] – user3488765 Nov 11 '16 at 09:45
  • Totally appreciate the suggestions Sherlock and user3488765. I agree there are solutions that will save a great deal of time but for educational purposes I would like to know how to rename an instantiated array. Appreciate any help you could give. – Phoenix Nov 11 '16 at 09:50

1 Answers1

0

You could retrieve the excel data in a separate method, then write the values into two arrays that you pass by reference. By setting the scope of all_data to this method only, as soon as it is exited the array will be deleted on the next garbage collector call. You can find more information here: How to delete an array. Something along these lines:

static void Main(string[] args)
    {
        object[][] arr1 = new object[10][];
        object[][] arr2 = new object[10][];
        GetData(ref arr1, ref arr2);
    }
    private static void GetData(ref object[][] arr1, ref object[][] arr2)
    {
        object[][] allData = new object[10][];
        object[] data;

        for (int i = 0; i < 10; i++) //this is just for sample purposes
        {
            data = new object[10];
            for (int j = 0; j < 10; j++) { data[j] = i * j; }
            allData[i] = data;
        }

        arr1 = allData;
        arr2 = allData;
    }
Community
  • 1
  • 1
Innat3
  • 3,561
  • 2
  • 11
  • 29