问题描述
|
我的代码中出现此错误
MedCareProviderLibrary.dll中发生了\'System.StackOverflowException \'类型的未处理异常
这是我的代码的片段,错误是从哪里来的。它在带有错误的零件上给出黄色箭头。
显示错误的部分以粗体显示。任何帮助将不胜感激谢谢
private string _TestNo;
private string _TestType;
private DateTime _TestDate;
private string _PatientNo;
private string _DoctorNo;
public test()
{
_TestNo = \"\";
_TestType = \"\";
_TestDate = new DateTime();
_PatientNo = \"\";
_DoctorNo = \"\";
}
public Test(string aTestNo,string aTestType,DateTime aTestDate,string aPatientNo,string aDoctorNo)
{
_TestNo = aTestNo;
_TestType = aTestType;
_PatientNo = aPatientNo;
_DoctorNo = aDoctorNo;
}
public string TestNo
{
set { _TestNo = value; }
get { return (TestNo); }
}
public string TestType
{
set { _TestType = value; }
**get { return (TestType); }
}
public DateTime TestDate
{
set { _TestDate = value; }
get { return (TestDate); }
}
public string PatientNo
{
set { _PatientNo = value; }
get { return (PatientNo); }
}
public string DoctorNo
{
set { _DoctorNo= value; }
get { return (DoctorNo); }
}
解决方法
您所有的属性获取器都将返回属性本身,而不是带下划线前缀的字段名称。
public string TestType
{
set { _TestType = value; }
get { return (TestType); }
}
而不是return _TestType
,而是执行return TestType
,因此属性getter会一次又一次地访问自身,从而导致无限递归并最终导致调用堆栈溢出。
另外,返回值不一定需要用方括号(除非您要计算某些复杂的表达式,在这种情况下您不需要)。
更改您的吸气剂,改为返回下划线前缀的字段(对所有属性执行此操作):
public string TestType
{
set { _TestType = value; }
get { return _TestType; }
}
如果您使用的是C#3.0,也可以像其他建议一样使它们成为自动属性。
,在ѭ5的属性中,您递归调用get
:
get {return TestNo; }
这没有终止的方法,并且会不断调用自己,直到堆栈被烧毁并且StackOverflowException
被抛出为止。
它应该是:
get {return _TestNo; }
如果在C#3.0及更高版本上,则可以使用“自动”属性,从而完全避免了该问题:
public string TestNo { get; set;}
这当然适用于您拥有的所有其他属性
,您必须在属性实现中返回支持字段,而不是属性本身,否则属性将递归调用自身并导致堆栈溢出:
public string TestNo
{
set { _TestNo = value; }
get {return _TestNo; }
}//End of TestNo Properties
由于您没有使用任何其他逻辑来要求您自己实现属性,因此建议您使用自动属性:
public string TestNo {get;set;}
,您以递归方式调用get函数,并且不引用要返回的对象。它看起来应该像这样:
public string TestNo
{
set { _TestNo = value; }
get {return _TestNo; }
}//End of TestNo Properties
public string TestType
{
set { _TestType = value; }
**get { return _TestType; }**
}//End of TestType Properties
public DateTime TestDate
{
set { _TestDate = value; }
get { return _TestDate; }
}//End of TestDate Properties
public string PatientNo
{
set { _PatientNo = value; }
get { return _PatientNo; }
}//End of PatientNo Properties
public string DoctorNo
{
set { _DoctorNo= value; }
get { return _DoctorNo; }
}//End of DoctorNo Properties
,您将返回属性而不是成员变量,从而导致递归。
例如:
public string TestType
{
set
{
_TestType = value;
}
get
{
return (TestType);
}
}
应该:
public string TestType
{
set
{
_TestType = value;
}
get
{
return _TestType ;
}
}
,因为您正在尝试返回属性本身(对get方法进行隐式调用),而该属性又试图一次又一次地返回,依此类推……因此您会得到堆栈溢出。
您的代码应为:
public DateTime TestDate
{
set { _TestDate = value; }
get { return _TestDate; }
}//End of TestDate Properties
或者,您可以使用自动属性:
public DateTime TestDate
{
set; get;
}//End of TestDate Properties
,您的属性返回自己,而不是成员变量,导致堆栈爆炸。您可能要写的是:
public string TestType
{
set { _TestType = value; }
get { return _TestType; }
}//End of TestType Properties