4

I am not sure if this is possible in c# but i'm having a variable defined in

 public partial class Form1 : Form
 {...
   ...
    #region used variables
    public ulong[] logP = {0,0,0,0,0,0,0,0}
  ....
  ..

Then later in the program i want have an option to adjust the size in the program before the main routines will launch over it and do some calculations

Because i like to test with vaious sets of numbers and array sizes i would like to have an option to resize the array, for each test (but this array isnt bound to a single procedure, its a global variable that needs to be resizable.

As the whole program at various parts is using this array, (under various functions) how should i put this part

    ulong [] sumP = new ulong[numericupdown.valeu];

So that it would change this global variable size ?

user613326
  • 2,140
  • 9
  • 34
  • 63

4 Answers4

21

You cannot resize an array; you must declare a new array of the desired size and copy the contents of the original array into the new array.

Update: I don't like Array.Resize - it doesn't resize the array (as the method name would suggest), it creates a new array and replaces the reference:

    static void Main(string[] args)
    {
        int[] a1 = new int[] { 1, 2, 3 };
        int[] a2 = a1;

        Console.WriteLine("A1 Length: {0}", a1.Length); // will be 3
        Console.WriteLine("A2 Length: {0}", a2.Length); // will be 3
        Array.Resize(ref a1, 10);
        Console.WriteLine("A1 Length: {0}", a1.Length); // will be 10
        Console.WriteLine("A2 Length: {0}", a2.Length); // will be 3 - not good

        Console.ReadLine();
    }
Moho
  • 15,457
  • 1
  • 30
  • 31
  • 4
    Refer to `Array.Resize` in the MSDN documentation. – staafl Oct 11 '13 at 22:32
  • 1
    Interesting, I never knew about that - it's just a shortcut to what I described, however, and leaves any other references to the original array the same as described in my update – Moho Oct 11 '13 at 22:38
  • in other words - you're not resizing the array (regardless of the name of the method), you're replacing the array. – Moho Oct 11 '13 at 22:39
  • +1 for proving that you'll get a new instance from the (ref x) keyword. And the original array isn't resized. This shows that an array it self cannot be resized (same like alloc/mallocs). The only thing you'll think it is resizing, because it copies the contents also. I would like to know why the other downvoted it... :) – Jeroen van Langen Oct 11 '13 at 22:44
0

as icemanind suggested - consider using a List array however as better practice, make sure you initialize the array capacity to a reasonable amount i.e:

List<string> myList = new List<string>(10); // initial capacity is 10

the advantage is performance and space allocation is predefined and it doesn't mean it needs to recreate the collection everytime you add an item to it as it would use the existing allocated amount until its reached then it increases it internally.

M. Ozn
  • 1,018
  • 1
  • 19
  • 42
Ahmed ilyas
  • 5,722
  • 8
  • 44
  • 72
0

Use Array.Resize() to resize an array,

http://msdn.microsoft.com/en-us/library/bb348051.aspx

For example:

        var intArray = new int[5] { 1, 2, 3, 4, 5 };

        Array.Resize(ref intArray, 10);
Kyle
  • 32,731
  • 39
  • 134
  • 184
0
    class Program
    {
       static void Main(string[] args)
       {
        var myArr = new int[5] { 1, 2, 3, 4, 5 };
        Array.Resize(ref myArr, 10);
        for (int i = 0; i < myArr.Length; i++)
        {
            Console.WriteLine("   [{0}] : {1}", i, myArr[i]);
        }
        Console.ReadLine();
    }
}
GANI
  • 2,013
  • 4
  • 35
  • 69