I am getting StackOverflow

What is wrong how come I am getting a StackOverflow and code will not execute correctly.

Here is my code:


public class Character
    {
        private string name;
        private string answer;
        private int life;
        private bool mHeal = false;
        private Random randomMoney = new Random();
        Character character = new Character();
 
        public int RandomMoney
        {
            get
            {
                return randomMoney.Next(100);
            }
        }
        public bool Heal
        {
            get
            {
                return mHeal;
            }
            set
            {
                mHeal = value;
            }
        }
        public string Name
        {
            get
            {
                return (this.name);
            }
            set
            {
                this.name = value;
            }
        }
        public int Life
        {
            get
            {
                return (this.life);
            }
            set
            {
                this.life = value;
            }
        }
        public int calcPoints()
        {
            return Life;
        }
        public void input()
        {
            Console.Write("Character's Name: ");
            character.Name = Console.ReadLine();
 
            Console.Write("Amount of life points: ");
            character.Life = int.Parse(Console.ReadLine());
 
            Console.Write("Will you be a healer?(true or false): ");
            answer = Console.ReadLine();
            if (answer.ToUpper() == "TRUE")
            {
                character.Heal = true;
            }
            else
                character.Heal = false;
        }
 
    }
    public class Program
    {
        static void Main(string[] args)
        {
            Character character = new Character();
 
            character.input();
 
            MessageBox.Show(string.Format("Character Name: {0}\nLifepoints: {1}\nHealer Class: {2}\nStarting Gold: {3}", character.Name, character.calcPoints(), character.Heal, character.RandomMoney));
 
        }
    }

Open in new window

jschmuffAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
because of this:

public class Character
    {
<...>
        Character character = new Character();  <<<< this line
<...>

you define, with that, that each character object has another character object as variable... that will do this recursively.


so, remove that line, and change also the "input" method
public void input()
        {
            Console.Write("Character's Name: ");
            this.Name = Console.ReadLine();
 
            Console.Write("Amount of life points: ");
            this.Life = int.Parse(Console.ReadLine());
 
            Console.Write("Will you be a healer?(true or false): ");
            answer = Console.ReadLine();
            if (answer.ToUpper() == "TRUE")
            {
                this.Heal = true;
            }
            else
                this.Heal = false;
        }
 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jschmuffAuthor Commented:
Thanks one quick question how can I turn input into a virtual function? would I go virtual void input()
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
>how can I turn input into a virtual function?
why would you want to do that?
0
jschmuffAuthor Commented:
because I plan on creating some derived classes like Warrior and such.
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
ok.
so, yes, indeed, virtual will be what you need to use.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.