I want to use a static List with an interface in .net
I wonder where in the code I should declare my List : in the interface class, in the derived classes, or somewhere else ?
For the moment I have it declared in the derived class (BacFlo).
My problem is : I can't populate the List correctly, it seems like the List is empty when it shouldn't.
Here is an extract of my code :
Interface :
public interface Serre
{
void Plant(Plante p);
void Remove(string s);
void Water(string s);
void Clone(string s);
void Inventory();
}
Derived Class #1 :
public class BacFlo : Serre
{
static List<Plante> Garden = new List<Plante>();
}
My main class :
Serre bc = new BacCro();
Serre bf = new BacFlo();
for (int i = 0; i < 10; i++)
{
Plante p = new Plante("Plante commune", "Vivace", "Une plante", 120);
bf.Plant(p);
bc.Plant(p);
}
for (int i = 0; i < 10; i++)
{
Plante p = new Plante("Plante rare", "à bulbe", "Une plante", 10);
bf.Planter(p);
bc.Planter(p);
}
bf.Inventory();
bc.Inventory();
}
void Serre.Inventory()
{
Console.WriteLine("");
Console.WriteLine("Inventaire du bac de floraison :");
foreach (Plante p in Jardin)
{
Console.WriteLine("Plante de nom " + p.Name + ", de type " + p.Type + ", de description " + p.Description + ", de taille " + p.Taille + ", Arrosée : " + (p.IsWet == true ? "Oui" : "Non"));
}
What I want to achieve is correctly populate the List with my for loop and have displayed the inventory with the appropriate method.
I'm not sure about the instanciation of a static List...