Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I Have array declarations

Texbox [] tb = new Texbox[]{txtbox1,txtbox2,.........}

where should i create this? should it be created/declared in the form load to make it accessbile in all functions/method of the same form?.


Thank you in advance for your help.


Thank you.
Posted

Declare this array at the top of the form (or rather outside all the methods in the form).
Load the array in the form's load event[^].

Edit: A better apporach would be to initialize the array in the form constructor.
Form_Load is not the best way to do things when we are using .Net.

Controls are initialized in the InitializeComponent() method so avoid defining the array in that method.
Let all the controls get initialized and then set the array.
 
Share this answer
 
v4
Comments
Sergey Alexandrovich Kryukov 1-Sep-14 23:48pm    
"Top of the form" it totally irrelevant. Do you think it's like in C++? It is not.
And then "avoid defining the array is super bad advise! I would rather advise to avoid using stupid idiotic Load event and InitializeComponent. Those are for stupid manual "graphical" programming. (No, I don't say it's always bad, but this is for ad-hoc manual programming, which is not always applicable.) OP is right: it array is needed, it should be required. Sorry, but this is the advice to ruin real programming and doing boring manual poorly supportable code.

—SA
Abhinav S 2-Sep-14 1:04am    
I thought you might have a comment on 'top of a form' and I agree that is not mandatory but only good practice - since its easier to read.
As a result, I had put my comment "outside the methods" in brackets. :)

Also, you might like to read my answer little more carefully. I'm not saying 'avoid defining array', I'm saying 'avoid defining array in InitializeComponent'.
Sergey Alexandrovich Kryukov 2-Sep-14 2:32am    
Yes, I misread it; sorry. But corrected understanding does not make you answer better, if not making it worse. Here is why: your advice not to declare it in InitializeComponent is misleading. Yes, of course one should not try this stupid thing, but who told you it could possibly happen, how? You are warning against something which hardly could have happen. This is an auto-generated code.

"Load the array in the form's load event" is just awful. Why do you think the array is "loaded"? Where? I understand that misunderstanding could come from the very misleading name of this (in fact fake) event, "Load", but this Microsoft awkwardness does not free us from understanding of what's going on.

—SA
Sometimes, it's a great idea to create an array if similar controls. It eliminates the need of having individual variable/member names, allows for traversing the array in a loop, and a lot more. Using the designer, if you have many controls, often means manual code, repeating the same stupid clicks, etc. This is the opposite to the main idea of programming. The designer use should be limited for very specific ad-hoc programming. Real serious software is created using more of the code. Please see my comment to Solution 1.

So, to start with, don't create your text boxes in the designer. This is not a best way to create an array.

Where to put it? It's totally up to you. You should know that if you declare an object in the class, all instance members (that is, non-static ones) of the class will have access to it. Where to initialize? In many places, but the array could be initialized at the point of declaration, and the elements could be initialized in some method called from the constructor (along with designer-generated InitializeComponents, if any):
C#
const int textBoxCount = // .. could be even variable, in more complex cases

TextBox[] myTextBoxes = new TextBox[textBoxCount];

Now, you need to initialize the elements in the loop. Let's assume you call it from constructor (but you can do it elsewhere):
C#
void InitializeMyTextBoxes(Control someParent) { // some parent could be form, Panel, TabPage, anything like that
    for (int index = 0; index < textBoxCount; ++index) {
        myTextBoxes[index] = new TextBox();
        myTextBoxes[index].Width = //... some width
        myTextBoxes[index].Left = //... may or may not depend on index and your layout
        myTextBoxes[index].Width = //... may or may not depend on index and your layout
        //... any other arrangements
        myTextBoxes[index].Parent = someParent; // this is the key, the way to add it
        // same as someParent.Controls.Add(myTextBoxes[index]);
    } //loop
} //InitializeMyTextBoxes


This way, you can access all elements of the array through the array object, myTextBoxes.

—SA
 
Share this answer
 
Comments
Abhinav S 2-Sep-14 2:58am    
SA I have gone through your answer. I realize it is different to what I'm saying.
The initial question is about creating textboxes from the designer.

In this scenario, you would not want to load textboxes till the form_load event fires, since you would not be sure if all the textboxes have been instantiated. So your answer is ok for a textboxes generated at runtime, but not if textboxes are being created at design time. An alternate approach would be to create this array at the end of InitializeComponent() method but that is going to be a maintenance nightmare.

I agree that constructor is a better way than form_load.
Sergey Alexandrovich Kryukov 2-Sep-14 11:21am    
Yes, I know. The only difference would be what OP says: TextBox[] myTextBoxes = new TextBox[] { textBox1, textBox2, ... }.
This is also possible (I should have told it), but creation the elements of array in much better. Actually, using the designer for everything can be a disaster in some cases, and the array of similar controls is one of those cases.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900