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

SolutionsCSCommented:
implement a property to access the member or make a public member wich is not the prefered way to expose data outside class.  

public class test {
  private int intValue;

  int Value
 {
  get { return intValue;}
  set { intValue = value;}
 }
}

or

public class test {
  public int intValue;
}
0
galar71Commented:
You can try something like the folowing:

using System;
using System.Collections.Generic;
using System.Text;
 
namespace InputIntTest1
{
    class IntValueContainer
    {
        private int myInternalIntValue;
 
        public int IntValue
        {
            get
            {
                return myInternalIntValue;
            }
            set
            {
                myInternalIntValue = value;
            }
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            IntValueContainer myIntValueStorage = new IntValueContainer();
 
            int intValue;
 
            Console.Write("Please enter a number: ");
            string strValue = Console.ReadLine();
            intValue = Convert.ToInt32(strValue);
            myIntValueStorage.IntValue = intValue;
            Console.WriteLine("You entered " + myIntValueStorage.IntValue);
            Console.ReadLine();
        }
    }
}

Open in new window

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
jschmuffAuthor Commented:
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

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

SolutionsCSCommented:
ReadLine return text.
validate the input and cast it to int
0
icrCommented:
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.
0
jschmuffAuthor Commented:
I get it output as a dollar value how do I get rid of that?
0
galar71Commented:
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.
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.