?
Solved

I am getting StackOverflow

Posted on 2008-02-02
5
Medium Priority
?
289 Views
Last Modified: 2010-04-15
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

0
Comment
Question by:jschmuff
  • 3
  • 2
5 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 20806218
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
 

Author Comment

by:jschmuff
ID: 20806247
Thanks one quick question how can I turn input into a virtual function? would I go virtual void input()
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20806258
>how can I turn input into a virtual function?
why would you want to do that?
0
 

Author Comment

by:jschmuff
ID: 20806272
because I plan on creating some derived classes like Warrior and such.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20806318
ok.
so, yes, indeed, virtual will be what you need to use.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
SingleRun is a tool that ensures that only one instance of an application is started, running it again brings the application to focus.
Is your organization moving toward a cloud and mobile-first environment? In this transition, your IT department will encounter many challenges, such as navigating how to: Deploy new applications and services to a growing team Accommodate employee…
The video provides a quick and easy steps to migrate MBOX file to well known Outlook PST and Office 365. Besides this, it also supports and migrates more than 20 email clients of MBOX which include AppleMail, Opera, Thunderbird and SeaMonkey effortl…
Suggested Courses

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question