hot like the ice
hot like the ice

Reputation: 19

error: cs0131 The left-hand side of an assingnment must be a variable, property or indexer

I've tried to make a text adventure and a got an error when I get to the "if" part. I got it to work once but not like I wanted it.

I changed it a bit and then gave up and went to the original script but it wasn't working like last time and instead gave me this error:

cs0131 The left-hand side of an assingnment must be a variable, property or indexer

Here is my code:

Console.WriteLine("What's your name");
string name = Console.ReadLine();
Console.Write("Hello " + name);
Console.WriteLine(" do you like games?");
Console.WriteLine("yes or no");
string yes = Console.ReadLine();
string no = Console.ReadLine();

if (Console.ReadKey() = yes) { Console.WriteLine("Great!, Lets play one"); }
//the error is at "if (console.readkey()"

Upvotes: 1

Views: 6969

Answers (2)

Draken
Draken

Reputation: 3189

Going on your current code, you probably should have something more like this:

Console.WriteLine("yes or no");
string answer = Console.ReadLine();

if (answer == "yes") { Console.WriteLine("Great!, Lets play one"); }

Major differences are:

  1. You are reading the answer a user types after asking yes or no, however in your code you are then trying to re-read another answer. Which doesn't quite make sense. The console will hang until the user enters another response.

  2. As JamesFaix said, you are then trying to assign a value to Console.ReadKey() of whatever the user responded with first, after you asked them if they want to play. Instead you should be checking if the user's response was a positive reply.

Upvotes: 1

JamesFaix
JamesFaix

Reputation: 8665

In C# and many languages, there is a distinction between assigning a variable is equal to a value and testing for equality.

In C# = is used to assign values. int x = 1; will create a variable with the value 1.

== is used to test a value, so you would write if (x == 100) { /* something */ }

Upvotes: 3

Related Questions