private void button6_Click(object sender, EventArgs e)
{
for (int i = 0; i < a.Length; i++)
{
MessageBox.Show(a[i]);
}
}
public void button7_Click(object sender, EventArgs e)
{
string[] a = { textBox1.Text};
}
Asked
Active
Viewed 1,652 times
-1

Ocaso Protal
- 19,362
- 8
- 76
- 83

Ashraf
- 1
- 1
-
1The error means that you have not declared "a" variable in "button6_Click" method. Note: "a" declared in "button7_Click" is not visible – Vitaliy Aug 23 '11 at 08:01
4 Answers
4
a
is a method variable; it only exists per call to button7_Click
. I suspect you need to make it a *field:
for (int i = 0; i < a.Length; i++)
{
MessageBox.Show(a[i]);
}
}
private string[] a;
public void button7_Click(object sender, EventArgs e)
{
a = new string[]{ textBox1.Text};
}
and then: choose a better name than a
.

Marc Gravell
- 1,026,079
- 266
- 2,566
- 2,900
1
a
is not in scope inside the button6 click.
You must declare it as a field, within that handler or pass it in within a custom eventargs.
You could do this also
private void button6_Click(object sender, EventArgs e) {
string[] a = { textBox1.Text};
for (int i = 0; i < a.Length; i++)
{
MessageBox.Show(a[i]);
}
}

halfer
- 19,824
- 17
- 99
- 186

Daniel Elliott
- 22,647
- 10
- 64
- 82
1
Because a is defined as a local variable for the button7_click function make it global over the form; define it on the variables of the form

Sara S.
- 1,365
- 1
- 15
- 33
0
you're calling "a.Length" in button6_Click but a is not defined in that method. if you need "a" in both methods you need to make it a class variable. but by the looks of this you could just as well get the contents of the textbox in method button6_Click.

mtijn
- 3,610
- 2
- 33
- 55