In some of my classes I would like to have the ability to iterate through the items using a foreach-loop. As long as I'm not using any static members, everything works fine:
public class Inventory
{
// ..lots of non-static methods...
public IEnumerator GetEnumerator ()
{
return m_items.GetEnumerator ();
}
// Private array holding a bunch of Items-classes
private ArrayList m_items = new ArrayList ();
}
I can then use my inventory-class this way:
foreach (Item item in Player.Inventory)
{
// Do something with the item
}
But now I have a problem using GetEnumerator on a class with static members. The class looks something like this:
public class Players
{
public static void Add (Player player)
{
m_players.Add (player);
}
// ...more static methods...
public static IEnumerator GetEnumerator ()
{
return m_players.GetEnumerator ();
}
// Hold a bunch of Player-classes
private static ArrayList m_players = new ArrayList ();
}
The error message is
'Players' denotes a 'class' where a 'variable' was expected. Do I have to use a delegate instead, or is there a way to fix this?