-2

I'm really new to c#, just knowing the basics.. I can't seems to find any solution to my problem I've tried every possible google search query.

So here's what I'm trying to do: I want to determined if 1 or 2 was pressed by the user.

 int action;
 Console.WriteLine("ACTIONS: Press 1 to do quest, Press 2 to access teleport");

I'm trying if else statement but I always getting

use of unsigned local variabled 'action'

Sunil Kumar
  • 3,142
  • 1
  • 19
  • 33
null
  • 1,062
  • 1
  • 9
  • 21
  • 2
    I think you might find your answer from an earlier question: [Reading an integer from user input](http://stackoverflow.com/questions/24443827/reading-an-integer-from-user-input) – cbr Jan 09 '17 at 11:12
  • This surely can't be your entire code? Where's the if-else-Statement? – waka Jan 09 '17 at 11:12
  • 2
    You should start with some C# book. No on SO. – honzakuzel1989 Jan 09 '17 at 11:13
  • The reason you're getting "use of unassigned local variable" is because you never set the value of `action`. – cbr Jan 09 '17 at 11:13
  • possible duplicate http://stackoverflow.com/questions/1100285/how-to-detect-the-currently-pressed-key – Afnan Ahmad Jan 09 '17 at 11:13
  • Like i said i have tried if else statement, but i just get an error 'use of unssinged local variabled 'action'. – null Jan 09 '17 at 11:14
  • if you know console.write, you should know console.read too. Check console.read and make GOOD research – onur Jan 09 '17 at 11:15
  • You didnt put anything in action thats why @JuanDelaCruz .. seriously read a c# book, this will be covered in the early chapters – BugFinder Jan 09 '17 at 11:19
  • your question need to be improved it's not well declared – Oghli Jan 09 '17 at 11:23
  • @BugFinder I don't see why i need to assigned a value to action, there will be 2 possible action..in c++ you just have to check if the variable is equal to 1 and then it will do the condition. – null Jan 09 '17 at 11:26
  • @JuanDelaCruz this is a closed question now, and you are missing the point, action is empty because you never did something with it.. it cant contain user input - please read some c# learning books – BugFinder Jan 09 '17 at 11:45
  • @BugFinder Maybe you need to read my question again. – null Jan 09 '17 at 12:37
  • @JuanDelaCruz I can assure you I do not. You didnt use it. – BugFinder Jan 09 '17 at 12:37
  • @BugFinder Tell me how assigning a value to 'action' will help. You should widen your phrase "didn't use it'. – null Jan 09 '17 at 12:41
  • let me repeat this loudly "THIS QUESTION IS CLOSED - DROP IT!" – BugFinder Jan 09 '17 at 12:43
  • @BugFinder just as I thought. – null Jan 09 '17 at 12:45

1 Answers1

0

You can parse the number by reading it. Like:

string stg = Console.ReadLine();
int i = Int.Parse(stg);
if(i == 1){
//some code
}
else{
//some other code
}

And the "error" you're getting is no error at all. The program is just saying that you've declared the integer type Action and has not used it.