0

I made a windows form application .sln in VS. It has a Program.cs, and Form1.cs. It builds. There's a button in Form1.cs

private void button1_Click(object sender, EventArgs e) { MessageBox.Show("Hello");}

I have an array in Program.cs

static void Main() {
string[] john = {"tom", "susan"};
}

I want my button to access this array so it can

MessageBox.Show(john[0]);

I don't understand all the namespaces and fully qualified classes and all that. I just couldn't stick my array in Form1.cs anywhere without getting an error, it had to go back in Main. Thanks for any help.

5 Answers5

2

Scope of your variable limited to main method, do as below

static class Program
{
   public static string[] john = { "tom", "susan" };

    [STAThread]
    static void Main()
    {

and then

private void button1_Click(object sender, EventArgs e)
{
    MessageBox.Show(Program.john[0]);
}

you can use public array declaration but you need to create instance of class in that case.

Damith
  • 62,401
  • 13
  • 102
  • 153
0

If you add the declaration of your variable john inside the form class, it should work.

namespace WindowsFormsApplication7 {
    public partial class Form1 : Form {
        string[] john = { "tom", "susan" };

        public Form1() {
            InitializeComponent();
        }

        // Rest of your code...
Maarten
  • 22,527
  • 3
  • 47
  • 68
0

Your array is out of scope in Form1 class. To rectify declare it public in Program:

public static string[] john = {"tom", "susan"};

Then access with:

MessageBox.Show(Program.john[0]);
Chris L
  • 2,262
  • 1
  • 18
  • 33
  • this wont compile. you have to create object of Progarm to access its non-static member. – Kurubaran Sep 23 '13 at 11:27
  • Ok, I understand this one the best from my level of experience. I had to move the array out of Main and add your changes. `namespace WindowsFormsApplication1 {static class Program {public static string[] john = {"tom", "susan"}; [STAThread] static void Main() {}` – בנימן הגלילי Sep 23 '13 at 11:32
0

You should put the array in Form1 class for your form controls to use it. As a rule of thumb, don't put properties in Program.cs as they would be "too much global", or in other words, limit the scope of variable to the absolute bare minimum for cleaner and clearer code.

public class Form1
{
    string[] john = {"tom", "susan"};

    private void button1_Click(object sender, EventArgs e) 
    {
        MessageBox.Show(this.john[0]);
    }
}

For more info, MSDN can help: Access Modifiers (C# Reference) also you might want to take a look at this SO answer regarding best practices.

Community
  • 1
  • 1
Alex
  • 23,004
  • 4
  • 39
  • 73
0

Make your array public static.

public static string[] john = {"tom", "susan"};

MessageBox.Show(Program.john[0]);

However, this is not to be considered as a good practice: Form1 should not reference members of Program because it introduces a circular dependency (Program -> Forms1 -> Program) and breaks encapsulation.

A better solution would be to store this list in a public property of Form1.

Benoit Blanchon
  • 13,364
  • 4
  • 73
  • 81