We help IT Professionals succeed at work.

creating a function that returns a value from a class

jschmuff
jschmuff asked
on
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

Comment
Watch Question

Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
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;
}

Author

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.
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
what about this
public int calcPoints()
{
     return this.Life;
}

Open in new window

Author

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?
Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009
Commented:
>does it need to go in the class Character?
yes, exactly.
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

Explore More ContentExplore courses, solutions, and other research materials related to this topic.