Member Avatar for Falcon25

Hi guys, I think this is a rather simple question but my brain has just decided to forget everything. So any help would be much appreciated.

I'm using a Windows form project. I have three classes and all I want to do is use a List declared and initialised in my Form1 class throughout the remaining two classes. I want the contents of this list to effect the contents of each one so essentially it is just one list being used passed around.

After trying to use get / set methods and eveything i've just tried declaring it as a public static list:

public static List<string> stageManagementList;

and this works great but I have to keep writing the namespace followed by the varible name each time. Is there any easier way of doing this? Thanks for your time and reading this.

You can put

using MyNamespace;

at the top of all of the modules (and new namespaces) using the static list.
You will still need to reference it by its class name.

Variable (in your case a generic list) declared as public static is one solution to use it all over the project.
But if has its own weaknesses, like that it stays in memory for as long as your project is alive. And if the variable becomes huge, it can eat a lot of memory.
If you want to free the memory you should set the field to null so that the object the field has been pointing to becomes eligible for GC.
This only points out something: try to avoid static variables.

-------
What I would do to hold same referecne through all the classes of a project, is to pass the class reference to all the other classes, this way, you can access to non-static public field (or property).
Example:

class ClassForVarialbe
{
     public List<string> stageManagementList;

     public ClassForVariable()
     {
         stageManagementList = new List<string>();
     }

     //...
     //you can even create a common method to do or check something     

}


class Class1
{

     void StartingMethod()
     {
          //here we`ll instantiate a new variable (generic list):
          ClassForVariable cfv = new ClassForVariable();
          //to access to the public variable and ADD into it, you can use the class referecne:
          cfv.stageManagementList.Add("one");

          //now open other class (and pass the referecne or a class where out variable is);
          Class2 c2 = new Class2(cfv); //pass a class referecne
          c2.AddToList(); //examle from class2 how to add into same list

          //now open class3 and print data from list out:
          Class3 c3 = new Class3(); 
          c3.ReadFromList(cfv); //pass a class referecne to a method only

          //NOTE: if you will do:
          cfv = new ClassForVariable(); //you will loose all the data from the list!!!
     }
}


class Class2
{
     ClassForVariable cfv2; //accessible variable to all the class2
     void MiddleMethod2(ClassForVariable _cfv2)
     {
         this.cfv2 = _cfv2;
     }

     public void AddToList()
     {
         cfv2.stageManagementList.Add("two");
     }
}

class Class3
{

     void MiddleMethod2()
     {
        //we could pass to constructor,
        //but this example only passes to some of the methods bellow
     }

     public void ReadFromList(ClassForVariable _cfv3)
     {
         foreach(string item in cfv3.stageManagementList)
              Console.WriteLine(item);              
     }
}

I hope you see what in the upper code is all about. The point is to keep the class alive (there the variable(s) is/are). As soon as you will use NEW keyword for a class, all data from class varaibles will be gone (reseted).

Member Avatar for Falcon25

Thanks guys this has really helped me out and taught me some new things. Mitja I never knew that about static methods stay in memory as long as the project is alive so I'm going to experiment with your technique of creating a new object of the varible class and passing a class reference.

I've got two strong answers for this so going to mark this thread as solved. Thanks very much for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.