1

In my current program I am building an array of objects and then populating it, however I need to then access this populated array from another function within the same class. In C I would do this by making the array global, but global variables dont exist in C# and when I try to use the "Static" parameter it says arrays cant be static.

namespace FormsTest1
{
    public partial class Form1 : Form
    {
        public int AppCount;
        public static applications[] appList;

        public Form1() //Main Entry point of program
        {
            IEnumerable<int> apps = VolumeMixer.EnumerateApplications();
            AppCount = apps.Count();
            int i = 0;
            applications[] appList = new applications[AppCount];
            foreach (int app in apps)
            {
                appList[i] = new applications();
                appList[i].setProcessID(app);
                appList[i].populate();
                i++;
            }
            for (int j = 0; j < AppCount; j++) { ChannelSelect1.Items.Add(appList[j].Name); }
        }

        private void ChannelSelect1_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (int k = 0; k < AppCount; k++)
            {
            if (ChannelSelect1.Text == appList[k].Name) //<-- This array is not the one I populate in Form1()
            { Channels[0] = appList[k].PID; }
        }
    }

    public class applications
    {
        public int PID;
        public string ProcessName;
        public string WindowName;
        public string Name;
        public string Path;

        public void setProcessID(int ID) { PID = ID; }

        public string getProcessName() { return ProcessName; }
        public string getWindowName() { return WindowName; }
        public string getName() { return Name; }
        public string getPath() { return Path; }

        public void populate()
        {
            //stuff
        }
    }
}   

I cant pass the array into the other functions cause they are event driven, and I need the index-ability of arrays.

How do I declare and populate an array of objects in one function, and then use that array in another function in the same class?

6
  • 1
    Please paste your Applications class. Commented Jan 7, 2018 at 7:42
  • Just added that Commented Jan 7, 2018 at 7:43
  • Are you getting a compiler error or runtime error? Post the error or exception text please. Commented Jan 7, 2018 at 7:46
  • the error is "An unhandled exception of type 'System.NullReferenceException' occurred" "Object reference not set to an instance of an object" This happens when I try to reference the array in the second function Commented Jan 7, 2018 at 7:49
  • This is an exception, which occurs at runtime and not a compiler error. Please put the line on which you are getting the error. Commented Jan 7, 2018 at 7:51

1 Answer 1

4

Change your constructor from

applications[] appList = new applications[AppCount];

to

appList = new applications[AppCount];

You should initialize your instance field instead of creating a new local one.

Btw: It is not necessary to make the array static.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you that fixed it! I dont know why I didn't see that before, it makes so much sense.

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.