Skip to content

How to access dynamic array on a different form?

John Beeman

Asked Jul 24, 2008, 12:58 PM EDT1 answer
Share
LinkedInX
I am trying to develop a small APP for my work, but I am having trouble finding information on the best way to pass a multidimensional array between forms.

Basically, I loop through an Excel file in form2 and put it to string array[worksheet.rows.count,12] (there are always 12 columns but unknown number of rows). Now, how can I access this array from form1? I know there is more than one way, which is primarily the reason I am having problems trying to find an answer by searching. Also, since the array is also 2000 lines+ and I am doing thousands of calculations within form1, not sure if one way would be faster than another.

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • +1
    Hello,

    All that you need to do is declare the array as a public static array. here is a basic example

        public partial class Form2 : Form
        {
            public static string[,] array;

            public Form2()
            {
                InitializeComponent();
            }

            private void Form2_Load(object sender, EventArgs e)
            {
                array = new string[2,2];
                array[0, 0] = "one, one";
                array[0, 1] = "one, two";
                array[1, 0] = "two, one";
                array[1, 1] = "two,two";

            }
        }

    then just call it on the first from with:

    MessageBox.Show(Form2.array[1,1]);
    +1