I got a console application where I get an Stack Overflow error when starting the application.
Main programm
class Program
{
public static void Main(string[] args)
{
Town town = new Town();
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
My problem is that I want to name all the buildings in the foreach loop with the List of all the building names but it's not working I suppose because it's causing an Stackoverflow and I don't know why. Is there any better way to do this or am I doing something wrong after all somewhere else?
public class Town
{
public Town town = new Town();
public List<Buildings> buildings = new List<Buildings>();
private List<string> buildingNames = new List<string>() {"Town_Hall", "Market", "Residences", "Mortician", "Bank", "Hotel", "Tailor", "Gunsmith", "General_Store", "Sheriff", "Well", "Gate", "Wall"};
public void ResetTown()
{
foreach (Buildings building in town)
{
int i = 0;
i++;
building.Name = buildingNames[i].ToString();
building.Level = 0;
}
}
public IEnumerator<Buildings> GetEnumerator()
{
return buildings.GetEnumerator();
}
}
public class Buildings
{
public string Name {get; set;}
public int Level {get; set;}
}
public Town town = new Town();is the problem. Why you have that line code?public Town town = new Town();does?