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开发者_JS百科 { _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.
In your properties on your get
you are calling the get
recursively:
get {return TestNo; }
This has no way to terminate and keeps calling itself until the stack is blown and the StackOverflowException
gets thrown.
It should be:
get {return _TestNo; }
You can use Automatic properties if on C# 3.0 and above instead, and avoid the problem altogether:
public string TestNo { get; set;}
This of course applies to all of the other properties you have
You have to return the backing field instead of the property itself in your property implementation, otherwise the property will call itself recursively and cause a stack overflow:
public string TestNo
{
set { _TestNo = value; }
get {return _TestNo; }
}//End of TestNo Properties
Since you are not using any additional logic that would require you to implement your properties yourself I would recommend using auto properties instead:
public string TestNo {get;set;}
You recursively call the get function and don't reference the object you want to return. It should look like this:
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
You are returning the property rather than the member variable thus causing recursion.
eg:
public string TestType
{
set
{
_TestType = value;
}
get
{
return (TestType);
}
}
should be:
public string TestType
{
set
{
_TestType = value;
}
get
{
return _TestType ;
}
}
because you're trying to return the property itself (which makes an implicit call to the get method) which in turn tries to return again and again and again and so on... so you get a stack overflow.
your code should be as such:
public DateTime TestDate
{
set { _TestDate = value; }
get { return _TestDate; }
}//End of TestDate Properties
alternatively you could use auto-properties:
public DateTime TestDate
{
set; get;
}//End of TestDate Properties
Your properties are returning themselves, rather than your member variable, cause the stack to explode. What you probably meant to write is:
public string TestType
{
set { _TestType = value; }
get { return _TestType; }
}//End of TestType Properties
精彩评论