Declaring 'Lists'
-
Evening all ! Can anyone tell how i declare a list in my class so all the methods can see/access it or is this not possible ? I've created a list in button code in a form -
List<Animal> zooAnimals = new List<Animal>();
but can't seem to figure out how to make it global so other methods can access it. I tried
public List <zooAnimals>;
but that doesn't work Thanks in advance
-
Evening all ! Can anyone tell how i declare a list in my class so all the methods can see/access it or is this not possible ? I've created a list in button code in a form -
List<Animal> zooAnimals = new List<Animal>();
but can't seem to figure out how to make it global so other methods can access it. I tried
public List <zooAnimals>;
but that doesn't work Thanks in advance
use the word static before the declaration. But then the list will belong to the class and not the object
-
Evening all ! Can anyone tell how i declare a list in my class so all the methods can see/access it or is this not possible ? I've created a list in button code in a form -
List<Animal> zooAnimals = new List<Animal>();
but can't seem to figure out how to make it global so other methods can access it. I tried
public List <zooAnimals>;
but that doesn't work Thanks in advance
Declare a class (public, static if you want) and define the list in that class. For example:
public static class MyList {
private static readonly List <zooAnimals> = new List<Animal>();;
public static List<Animal> ZooAnimals {
get {
return zooAnimals ;
}
}
}The need to optimize rises from a bad design. My articles[^]
-
Declare a class (public, static if you want) and define the list in that class. For example:
public static class MyList {
private static readonly List <zooAnimals> = new List<Animal>();;
public static List<Animal> ZooAnimals {
get {
return zooAnimals ;
}
}
}The need to optimize rises from a bad design. My articles[^]
readonly is not recommended for objects of this sort since it implies the objects of the list are unchangeable when in fact they are. It is a constantly source of errors for novices.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
Most of this sig is for Google, not ego. -
readonly is not recommended for objects of this sort since it implies the objects of the list are unchangeable when in fact they are. It is a constantly source of errors for novices.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
Most of this sig is for Google, not ego.When posting the answer I though for awhile if I should declare the list readonly or not. When used correctly, readonly eliminates certain problems that can occur if the actual list is changed. I would say that it depends if you want the list to be changed or just to allow to modify it's content. That's why I also defined only getter for the property. The other thing was that the original question was, how to define a list globally so that others can see and use it. Based on the question my interpretation was (maybe incorrectly) that the list should not be changeable, but it's contents can still be modified.
The need to optimize rises from a bad design. My articles[^]