StackOverflowException Error

I have 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 comes from. He gives a yellow arrow to pieces with an error.

The error part is shown in bold. Any help would be greatly appreciated. Thank you.

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); } } 
+5
source share
7 answers

All your getters properties return the properties themselves, not field names with prefix prefixes.

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

Instead of return _TestType you perform return TestType , so the getter property continues to be accessed again and again, which leads to infinite recursion and, ultimately, to an overflow of the call stack.

In addition, parentheses are not necessary for the return values ​​(unless you are evaluating some complex expression that you are not in this case).

Modify your getters to use prefix 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 use C # 3.0.

+18
source

In your properties on get you call get recursively:

  get {return TestNo; } 

This has no way to end and keep on calling until the stack explodes and a StackOverflowException is thrown.

It should be:

  get {return _TestNo; } 

You can use Automatic properties if in C # 3.0 and higher, and generally avoid the problem:

 public string TestNo { get; set;} 

This, of course, applies to all other properties that you have.

+6
source

You must return the backing field instead of the property itself in your property implementation, otherwise the property will select 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 requires you to implement your properties, I would recommend using automatic properties instead:

 public string TestNo {get;set;} 
+5
source

You call the get function recursively and do not 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 
+1
source

You are returning a property, not a member variable, thereby causing recursion.

eg:

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

it should be:

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

because you are trying to return the property itself (which makes an implicit call to the get method), which in turn is trying to return again and again and again and so on ... so that you get a stack overflow.

your code should be like this:

 public DateTime TestDate { set { _TestDate = value; } get { return _TestDate; } }//End of TestDate Properties 

alternatively you can use automatic properties:

 public DateTime TestDate { set; get; }//End of TestDate Properties 
+1
source

Your properties are returned, not your member variable, causing the stack to explode. What you probably wanted to write:

 public string TestType { set { _TestType = value; } get { return _TestType; } }//End of TestType Properties 
0
source

All Articles