Click here to Skip to main content
12,251,332 members (51,548 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
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:

var city = new city(5);

and this is a class
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 22-Dec-12 5:00am
h5h6692
Edited 22-Dec-12 5:02am
ProgramFOX181.9K
v2
Comments
Krunal Rohit 22-Dec-12 11:06am
   
enter from tast ??

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Hi,

Try this:
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.
  Permalink  
Comments
h5h6 22-Dec-12 11:52am
   
thnx it works. :)
ProgramFOX 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 findSolutions(int city, List allSolutions)
{
if (city> Boardi.N)
return allSolutions;
ProgramFOX 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.... !!
ProgramFOX 22-Dec-12 12:27pm
   
And what's the problem?
h5h6 22-Dec-12 12:34pm
   
the same to input the character from tastier:
here is the clas
class Boardi
{
public static int N;

private int[] board;
private int col;
public Boardi()
{
board = new int[N + 1];
city= 1;
}
and how to give a number to the parameter N
I tried with object:
Boardi p=new Boardi()
and then p.N to direct to N..... but dont work.......
ProgramFOX 22-Dec-12 12:36pm
   
Like this:
Boardi.N = ...

Change ... into your integer.
h5h6 22-Dec-12 12:51pm
   
int value;
value= Boardi.N;
value= int.Parse(Console.ReadLine());
but dont work
I think in a class Boardi maybe we must do something... because parameter N should take value...
ProgramFOX 22-Dec-12 12:53pm
   
No, do this:

Boardi.N = int.Parse(Console.ReadLine());
h5h6 22-Dec-12 12:56pm
   
thnx thnx thnx....I appreciate that :)
It Works man :)
ProgramFOX 22-Dec-12 12:57pm
   
You're welcome!

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160426.1 | Last Updated 22 Dec 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100