-4

I am working on a multi-form program in C#, and would like to have arrays created whenever the program is run, as follows

private void Form1_Load(object sender, EventArgs e)
{
    GlobalVariables();
    MessageBox.Show("Number Of Users Check", 
      "There are " & Form1.AllAdmins.Length    & " Admins" &
      "There are " & Form1.AllLecturers.Length & " Lecturers" &
      "There are " & Form1.AllStudents.Length  & " Students");

}

public void GlobalVariables()
{
    UserAdmin[] AllAdmins = new UserAdmin[0];
    UserLecturer[] AllLecturers = new UserLecturer[0];
    UserStudent[] AllStudents = new UserStudent[0];
}

According to Visual Studio my arrays do not exist.

How can I create these arrays in a way that would allow me to add data to them later, on a different form?

All of the above has changed, as i now understand that it would not work how i was hoping it would work. I now understand that global variables are a bad idea, and have moved over to having a list of items that is created on form load, as it allows us to add items to the list later.

ds391
  • 1
  • 3
  • http://c2.com/cgi/wiki?GlobalVariablesAreBad http://programmers.stackexchange.com/questions/148108/why-is-global-state-so-evil http://stackoverflow.com/questions/10525582/why-are-global-variables-considered-bad-practice-javascript – Brandon Mar 08 '16 at 20:30
  • Does this even compile? AllAdmins is scoped to the method & will not exist outside of that method. – MatthewMartin Mar 08 '16 at 20:31

3 Answers3

0

First of all please don't use globals. Is a bad (worst) practice.

Second to do what you want you need to create a static class like this.

public static class MyGlobalArrays {
    public static UserAdmin[] AllAdmins;
    public static UserLecturer[] AllLecturers;
    public static UserStudent[] AllStudents;
}

You access your arrays like this: MyGlobalArrays.AllAdmins

In your program you cannot access your arrays because the scope you define them is local. They are only accessible in the method you have them in your case inside GlobalVariables().

CodeArtist
  • 5,534
  • 8
  • 40
  • 65
0

You could create them as public and static as fields of your class.

But it can lead you to problems since every class can change their content.

By the way, it's better to use lists instead arrays because it's easier to add data to them later.

public static List<UserAdmin> AllAdmins = new List<UserAdmin>();
Fabio
  • 3,020
  • 4
  • 39
  • 62
  • The lists vs arrays comment isn't entirely true. Lists just have extended functionality and allow arrays to be '"dynamic" - however there are times in which this isn't required and having a defined array will be a better suit. With that said, for most situations you are correct. – Gabe Mar 08 '16 at 20:47
0

Your array variables fall out of scope once the GlobalVariables() method completes.

WARNING: As a heuristic, ‘global variables are evil’ should serve to remind you to think through your problem and see if using global variables could potentially come back and bite you. With mutable global variables, it's possible for the values to be changed out from underneath you when you don't expect it. However, if your're not doing any parallel or asynchronous programming, then this risk is somewhat diminished.

Realistically, you should avoid using mutable global variables 95% of the time. You should really think-through how you want your code to function.

That being said, if you want the Form1_Load function to have visibility to your arrays, you will need to restructure you code like this:

UserAdmin[] AllAdmins = new UserAdmin[0];
UserLecturer[] AllLecturers = new UserLecturer[0];
UserStudent[] AllStudents = new UserStudent[0];

private void Form1_Load(object sender, EventArgs e)
{
      GlobalVariables();
      MessageBox.Show("Number Of Users Check", 
        "There are " & Form1.AllAdmins.Length    & " Admins" &
        "There are " & Form1.AllLecturers.Length & " Lecturers" &
        "There are " & Form1.AllStudents.Length  & " Students");
}
Robert
  • 371
  • 1
  • 7