We help IT Professionals succeed at work.

Storing an int variable in a class

jschmuff
jschmuff asked
on
Medium Priority
248 Views
Last Modified: 2012-05-05
How do I store an int variable in a class that is input from the user from the main code?
Comment
Watch Question

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

Author

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

ReadLine return text.
validate the input and cast it to int
icr

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

Author

Commented:
I get it output as a dollar value how do I get rid of that?

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

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