creating a function that returns a value from a class

How can I create a function to return a value from a class?

I want to return lifepoints through a function something like this

public void calcPoints()
{
     return lifepoints;
}

Here is my code I have:


public class Character
    {
        private string name;
        private int life;
        private bool mHeal = false;
        Random randomMoney = new Random();
 
        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 class Program
    {
        static void Main(string[] args)
        {
            Character character = new Character();
 
            string answer;
 
            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;
 
            MessageBox.Show(string.Format("Character Name: {0}\nLifepoints: {1}\nHealer Class: {2}", character.Name, character.Life, character.Heal));
        }
    }

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:
your code looks fine so far, except:

public void calcPoints()
{
     return lifepoints;
}

should be like this (specify a data type for the return):

public int calcPoints()
{
     return lifepoints;
}
0
jschmuffAuthor Commented:
that was just an example cause I wasnt sure how it needs to look. That doesnt work I tried using return character.Life; but no luck I have to access the value in the class and return the value.
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
what about this
public int calcPoints()
{
     return this.Life;
}

Open in new window

0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

jschmuffAuthor Commented:
That doesn't seem to work either, what could this problem be maybe it is where I am inserting the function. Where should I put it? I have tried inside the main and outside does it need to go in the class Character?
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
>does it need to go in the class Character?
yes, exactly.
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
Arthur_WoodCommented:
how does the value

lifepoints

differ from your defined function Life?

What is calcPoints supposed to be doing?

AW



public class Character
    {
        private string name;
        private int life;
        private bool mHeal = false;
        Random randomMoney = new Random();
 
        public bool Heal
        {
            get
            {
                return mHeal;
            }
            set
            {
                mHeal = value;
            }
        }
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
        public int Life
        {
            get
            {
                return life;
            }
            set
            {
                life = value;
            }
        }
    }

Open in new window

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.