I keep getting a stack overflow exception with this. I've narrowed it down to this class to try and figure out what was wrong but I simply have no idea why I keep getting this error message? Originally I have the user interface in another class but just to eliminate everything else like problems in my calling methods I moved the essentials to this class to try and figure out what was wrong. I thought it might be my properties? Maybe it's obvious to everyone else but I simply don´t understand.
Since I am very new to programming I would appreciate some help on what I have done wrong. From what I understand this problem occurs when you have something like an infinite loop?
namespace MyNameSpace
{
public class Customers
{
private List<Customers> customers;
public Customers()
{
customers = new List<Customers>();
AddCustomer(new Customers()
{
Name = "A", Telephone="1"
});
}
public string Name
{
get;
set;
}
public string Telephone
{
get;
set;
}
public void RunTest()
{
Console.WriteLine();
Console.WriteLine("****** VIDEOSTORE ******");
Console.WriteLine();
Console.WriteLine("1. Show Customers");
Console.WriteLine("6. Quit");
string userChoice = Console.ReadLine();
switch (userChoice)
{
case "1":
View();
break;
break;
case "2":
break;
}
}
public void View()
{
foreach (Customers c in customers)
{
Console.WriteLine();
Console.WriteLine(c.Name);
Console.WriteLine(c.Telephone);
Console.WriteLine();
}
}
public void AddCustomer(Customers custom)
{
customers.Add(custom);
}
}
}
In the Customers constructor you calling the Customers constructor again, creating infinite recursion.
You should have a separate class for a list of Customers and for a single Customer:
namespace MyNameSpace
{
public class Customer
{
public string Name
{
get;
set;
}
public string Telephone
{
get;
set;
}
}
public class Customers
{
private List<Customer> customers;
public Customers()
{
customers = new List<Customer>();
AddCustomer(new Customer()
{
Name = "A", Telephone="1"
});
}
public void RunTest()
{
Console.WriteLine();
Console.WriteLine("****** VIDEOSTORE ******");
Console.WriteLine();
Console.WriteLine("1. Show Customers");
Console.WriteLine("6. Quit");
string userChoice = Console.ReadLine();
switch (userChoice)
{
case "1":
View();
break;
break;
case "2":
break;
}
}
public void View()
{
foreach (Customer c in customers)
{
Console.WriteLine();
Console.WriteLine(c.Name);
Console.WriteLine(c.Telephone);
Console.WriteLine();
}
}
public void AddCustomer(Customer customer)
{
customers.Add(customer);
}
}
}
You are calling your creating a new Customers object in the Customers constructor.
Your constructor for Customers calls itself, causing an endless loop.
public Customers()
{
customers = new List<Customers>();
AddCustomer(new Customers() // <- Here
{
Name = "A", Telephone="1"
});
}
Endless recursive calls to a function will cause a StackOverFlow.
You are instantiating a List within the constructor of your Customers class. This will cause an infinite loop and result in a stack overflow.
I think you should try to separate your code into multiple classes.
public class Customer
{
public string Name { get; set; }
public string Telephone { get; set; }
}
public class Program
{
private List<Customer> _customers = new List<Customer();
public Program()
{
_customers.Add(new Customer()
{
Name = "A", Telephone="1"
});
}
// your other methods here - like View()
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With