use*_*537 5 c# exception-handling visual-studio-2010
我不断得到一个堆栈溢出异常.我已经把它缩小到这个类来试图弄清楚出了什么问题,但我根本不知道为什么我一直收到这个错误信息?最初我在另一个类中有用户界面,但只是为了消除其他一切,比如我的调用方法中的问题,我将基本要素移到这个类中,试图弄清楚出了什么问题.我以为这可能是我的财产?也许对其他人来说很明显,但我根本不明白.
由于我对编程很陌生,所以我会很感激我对错误的帮助.根据我的理解,当你有无限循环之类的东西时会出现这个问题?
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
在Customers构造函数中,再次调用Customers构造函数,创建无限递归.
您应该为客户列表和单个客户提供单独的类:
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
274 次 |
| 最近记录: |