Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hi, does any one can tell me how to give the value from keyboard not to be static in application, this is the part of code here is value static:

C#
var city = new city(5);


and this is a class
C#
class city
        {
            private readonly int[] _num;
            private readonly int _size;

           
            public city(int Size)
            {

                _size = Size;
                _num= new int[_size];
            }


this is just to adapt you with code.... this number that is in new city(5). I want to enter from tast,,but I cant...
Posted
Updated 22-Dec-12 5:02am
v2
Comments
[no name] 22-Dec-12 11:06am    
enter from tast ??

1 solution

Hi,

Try this:
C#
int cityInt = -1;
try
{
     cityInt = Convert.ToInt32(Console.ReadLine());
}
catch
{
     Console.WriteLine("Input isn't an integer!");
}
if (cityInt != -1)
{
      var city = new city(cityInt);
}

Hope this helps.
 
Share this answer
 
Comments
h5h6 22-Dec-12 11:52am    
thnx it works. :)
Thomas Daniels 22-Dec-12 11:53am    
You're welcome!
h5h6 22-Dec-12 12:12pm    
hey man, I change the way of code and again problem to give value from keyboard:
this is a class:
class Boardi
{
public static int N;

private int[] board;
private int col;
public Boardi()
{
board = new int[N + 1];
city= 1;
}

and a list:
static List<boardi> findSolutions(int city, List<boardi> allSolutions)
{
if (city> Boardi.N)
return allSolutions;
Thomas Daniels 22-Dec-12 12:15pm    
To get input, use this line:
Console.ReadLine();
That returns a string. To convert it to an int, use Convert.ToInt32:

int cityInt = Convert.ToInt32(Console.ReadLine());
h5h6 22-Dec-12 12:25pm    
Yes I use int.parse the same thing.
but i that second case here is different to input value from keyboard because I have a list as u see.... !!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900