StackOverflowException was unhandled

.net, c#, exception, stack-overflow

Solution

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.

Problem

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

Original source