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); }
}
NET Framework 2.0, you can't catch a StackOverflowException object with a try / catch block, and the corresponding process is terminated by default. Consequently, you should write your code to detect and prevent a stack overflow.
StackOverflowException is thrown for execution stack overflow errors, typically in case of a very deep or unbounded recursion. So make sure your code doesn't have an infinite loop or infinite recursion. StackOverflowException uses the HRESULT COR_E_STACKOVERFLOW, which has the value 0x800703E9.
A StackOverflowException is thrown when the execution stack overflows because it contains too many nested method calls. using System; namespace temp { class Program { static void Main(string[] args) { Main(args); // Oops, this recursion won't stop. } } }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With