blah blah blah is here! blah blah » Close

up0down
link

Hello

I want to have a multi-dimensional array that I want to declare such that I can access it from all the other functions in the program. I am designing a windows application. I have a form with 16 tabs, and i want to be able to declare an array of textbox ( which i know how to do now). I dont want to declare it in every function that i use. Where exactly should i declare it to be able to access it from everywhere else?

Thanks
Muhu

last answered 2 years ago

1 answers

up1down
link

I'd create a static class containing all your global variables. For example:

public static class Global

{
public static TextBox[,] TextBoxes;
}

This array can now be accessed from any other class/method in the application using the syntax: Global.TextBoxes

Don't forget to initialize it first before you try to access its elements, otherwise it will have the default value of 'null'.

Muhu
0

Umm. so okay, if i create this array with 16X16 textboxes, cant i just initialize it once in the Global class? I just dont want to have to initialize it in every single class. I am trying to find a manageable way to code the application since i have one form with 16 tabs that just do the same thing but for 16 different boards. I am thinking if i initialize the multi-dimentional array once, then maybe for each of the tab which had a Enter button, i can perform the same action under the Enter button, only by changing the indexing of the array. Let me know if this sounds right. Thank you Vulpes for you help, i m liking C# more than ever! Thanks again Muhu

vulpes
17279

Well, if you know that the array will always be 16 x 16, then you can create the array object within the global class (public static TextBox[,] TextBoxes = new TextBox[16,16];) and then fill the array with TextBox references from the Form which contains the tabs - perhaps in its Form_Load eventhandler which will only run once. If another class were to attempt to access an element of the array before it had been filled, then the element would of course be null.

Feedback