Link to home
Start Free TrialLog in
Avatar of jschmuff
jschmuffFlag for United States of America

asked on

Storing an int variable in a class

How do I store an int variable in a class that is input from the user from the main code?
SOLUTION
Avatar of SolutionsCS
SolutionsCS

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jschmuff

ASKER

I did something similar to that and I am getting an error:

Program.cs(62,30): error CS0029: Cannot implicitly convert type 'string' to 'int'

Here is my code:
    public class Character
    {
        private string name;
        private int life;
        private bool mHeal = false;
        Random randomNumber = 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();
 
            Console.Write("Character's Name: ");
            character.Name = Console.ReadLine();
 
            Console.WriteLine();
            Console.Write("Your name is {0:C}", character.Name);
            Console.WriteLine();
 
            Console.Write("Amount of life points: ");
            character.Life = Console.ReadLine();
 
            Console.WriteLine();
            Console.Write("Your available lifepoints are {0:C}", character.Life);
            Console.WriteLine();
 
        }
    }

Open in new window

Avatar of SolutionsCS
SolutionsCS

ReadLine return text.
validate the input and cast it to int
That's because Console.ReadLine() returns a string (as it reads a string of characters from the console). In order to assign it to an integer property you must first convert it into an int.
The best way to do that for int's is to use the Parse method:
character.Life = int.Parse(Console.ReadLine());
The string that is gotten from Console.ReadLine() is passed into the int.Parse function, which turns it into an integer (if it is indeed an integer representation) that you can then assign to character.Life.
I get it output as a dollar value how do I get rid of that?
As you can see of my example, I convert the string value that's output from Console.ReadLine(). You should however use a try/catch block around it to avoid any problems with empty input or non-integer input.