• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 199
  • Last Modified:

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

0
jschmuff
Asked:
jschmuff
  • 3
  • 2
1 Solution
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
 
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

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now