Process is terminating due to StackOverflowException

Kissen

Member
Joined
Jun 20, 2019
Messages
13
Programming Experience
Beginner
I don't understand why I get an error. I get: Process is terminating due to StackOverflowException.
C#:
using System;
namespace RPGtest
{
    
    class Program
    {
        public class BaseCharacter{
        private int health {get{return health;} set{
            if(health > 100) health = 100;
            else if(health <= 0) health = 0;
            else health = value;
        }}
        int _health{get{return health;} set{}}
        
        public void Damage(int value2) {
         health -= value2;
        }
        public void Print() {
            System.Console.WriteLine(health);
        }
    }
        static void Main(string[] args)
        {
           BaseCharacter character1 = new BaseCharacter();
           character1.Damage(50);
           character1.Print();
        }
    }
}
 
Actually everything here:
C#:
        private int health {get{return health;} set{
            if(health > 100) health = 100;
            else if(health <= 0) health = 0;
            else health = value;
        }}
will cause infinite recursion whenever health is read or written to. The calls to Damage() and Print() try to read/write to health

Let me reformat to make easier to read (and discuss)
C#:
private int health
{
    get
    {
        return health;
    }

    set
    {
        if (health > 100)
            health = 100;
        else if (health <= 0)
            health = 0;
        else
            health = value;
     }
}

Whenever health is read, the getter starting at line 3 is called. Line 5 within the getter tries to read health so the getter on line 3 is called again. Each call takes up stack space.

When trying to set a value for health, execution starts at the setter on line 8. The setter tries to read the value of health on line 10, so it calls the getter on line 3. See previous paragraph.
 
Last edited:
Ok, I understand that that infinite loop is very bad; and that the code cannot fully execute with this error. But I have no clue how to fix it.
 
You started down the solution by declaring _health as a backing field. The problem looks to be you then converted it into a property instead of a field by giving it setters and getters. Anyway the idea is that you property health should be doing its own reads and writes to the backing field _health.
 
As a quick aside, to help keep yourself from getting confused, you should follow the .NET framework naming convention. Properties and methods should be in Pascal case, while variables/fields should be in camelCase. That will help you distinguish between the property Health and the field health.

Alternatively, you can follow the Hungarian naming convention of prefixing "_" or "m_" before your fields, but the .NET Framework naming convention discourages using Hungarian. Not that it stops people from ignoring the recommendation and using "chkApproved" or "btnOk" just because they are used to Win32 programming and its extensive use of Hungarian.
 
Good call on the naming conventions. I fall victim to repeating the same steps myself sometimes. But I too am trying to break old habits as I sometimes still do it. :p
 
Back
Top Bottom