[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

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?
0
jschmuff
Asked:
jschmuff
  • 2
  • 2
  • 2
  • +1
2 Solutions
 
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
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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

Featured Post

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.

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