StackOverflowException是未处理未处理、StackOverflowException

2023-09-05 00:26:19 作者:ず仟種煭酒

我在我的code有此错误

  

型System.StackOverflowException的未处理的异常MedCareProviderLibrary.dll发生

下面是我的code一个片段,其中误差的来源。它提供了对有错误的部分黄色箭头。

部分显示错误是大胆的。任何帮助将大大AP preciated谢谢

 私人字符串_TestNo;
私人字符串_TestType;
私营的DateTime _TestDate;
私人字符串_PatientNo;
私人字符串_DoctorNo;

公开测试()
{
    _TestNo =;
    _TestType =;
    _TestDate =新的日期时间();
    _PatientNo =;
    _DoctorNo =;
}

公开测试(字符串aTestNo,串aTestType,日期时间aTestDate,串aPatientNo,串aDoctorNo)
{
    _TestNo = aTestNo;
    _TestType = aTestType;
    _PatientNo = aPatientNo;
    _DoctorNo = aDoctorNo;
}

公共字符串TestNo
{
    集合{_TestNo =价值; }
    {返回(TestNo); }
}

公共字符串TestType
{
    集合{_TestType =价值; }
    ** {返回(TestType); }
}

公开日期时间TestDate
{
    集合{_TestDate =价值; }
    {返回(TestDate); }
}

公共字符串PatientNo
{
    集合{_PatientNo =价值; }
    {返回(PatientNo); }
}

公共字符串DoctorNo
{
    集合{_DoctorNo =价值; }
    {返回(DoctorNo); }
}
 

解决方案

您所有的财产的getter在返回属性本身,而不是underscore- prefixed字段名。

 公共字符串TestType
{
    集合{_TestType =价值; }
    {返回(TestType); }
}
 
发生了未处理异常 System.StackOverflowException ,发生位置是w3wp.exe

而不是的返回_TestType ,你这样做返回TestType ,所以属性getter保持连连访问本身,导致无限递归并最终调用堆栈的溢出。

另外,返回值并不一定需要括号(除非你正在评估一些复杂的EX pression,在这种情况下,你都没有)。

更改您的getter方法​​返回underscore- prefixed领域,而不是(这样做的所有属性):

 公共字符串TestType
{
    集合{_TestType =价值; }
    {返回_TestType; }
}
 

或使它们自动属性中为他人建议,如果你使用C#3.0。

I'm having this error in my code

An unhandled exception of type 'System.StackOverflowException' occurred in MedCareProviderLibrary.dll

Here is a snippet of my code and where the error is coming from. It gives a yellow arrow on the part with the error.

The part showing the error is in bold. Any help will be much appreciated Thanks

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); }
}

解决方案

All your property getters are returning the properties themselves instead of the underscore-prefixed field names.

public string TestType
{
    set { _TestType = value; }
    get { return (TestType); }
}

Instead of return _TestType, you do return TestType, so the property getter keeps accessing itself again and again, resulting in infinite recursion and eventually an overflow of the call stack.

Also, return values don't necessarily need the brackets (unless you're evaluating some complex expression, which in this case you aren't).

Change your getters to return the underscore-prefixed fields instead (do this for all your properties):

public string TestType
{
    set { _TestType = value; }
    get { return _TestType; }
}

Or make them automatic properties as others suggest if you're using C# 3.0.