StackOverflowException未处理 [英] StackOverflowException was unhandled

查看:103
本文介绍了StackOverflowException未处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码中出现此错误


在MedCareProviderLibrary.dll中发生了一个未处理的类型System.StackOverflowException的异常/ p>

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

而不是 return _TestType ,你做返回TestType ,所以属性getter会一遍又一遍的访问自身,导致无限递归,最终调用堆栈溢出。

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).

更改您的getter以返回下划线前缀字段(对所有属性执行此操作):

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

或使他们自动属性,其他人建议您使用C#3.0。

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

这篇关于StackOverflowException未处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆