0

New C# user. I understand global variables are not part of C#. Been trying learning properties, set and get. But I have what must be a common situation and I can't find the answer. This is what normally seems to happen when I design a form:

...
private void Form1_Load(object sender, EventArgs e) {         
...
    private void Form1_Load(object sender, EventArgs e)
        {....Label[]  labelsArray = { label1, label2, label3, label4, label5 };...
        }

    private void button1_Click(object sender, EventArgs e)
        {
            ...
        }
...
}
...

So I made a "labelsArray" in Form1_Load that I want to use in the button1_Click. But I cannot - "Error 1 The name 'labelsArray' does not exist in the current context".

I do have access to the labels in button1_Click and I suppose I can re-declare the array and it'd work. But seems like I should be able to pass the array from Form1_Load to button1_Click and use it. But I'm lost after trying many things. How is it done please?

Thanks

1
  • Declare your label[] globally outside Form Load method. Commented Sep 13, 2014 at 5:16

1 Answer 1

2

All you need to do is to declare the variable outside the methods like:

private Label[]  labelsArray = null;

private void Form1_Load(object sender, EventArgs e){
   labelsArray = new Label[]{ label1, label2, label3, label4, label5 };
}

private void button1_Click(object sender, EventArgs e){}
Sign up to request clarification or add additional context in comments.

7 Comments

We're not writing VB6 code anymore, .NET supports constructors. The labelsArray initialization belongs in the constructor, after the InitializeComponent() call.
The array just contains references, they are initialized by the constructor.
No, they are not null after InitializeComponent(). Intentionally putting initialization code in the Load event makes programs slower. It defeats the bulk initialization that Winforms can use when it knows how to initialize the native window up front. The Load event is only necessary when you need to know the Location and Size properties. Thinking that Load is "special" is cargo cult, it came from VB6 and got re-inforced by the designer making the event the default event. It is very hard to root out. What you ultimately failed to show is how using the constructor could be wrong.
If you don't have enough info then always post correct code.
Thanks, but can't figure out. I tried exactly what cited in answer1 and get errors - so I tried the following and get one error. I do: private Label[] labelsArray = null; were you say, then after some tries I do: labelsArray = new Label{ label1, label2, label3, label4, label5 }; and get the error: Error 1 Cannot initialize type 'System.Windows.Forms.Label' with a collection initializer because it does not implement 'System.Collections.IEnumerable' Be grateful for help...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.