Dominic Slack
Dominic Slack

Reputation: 3

the left hand side of an assignment must be a variable property or indexer

I am trying to create some code that determines if the a and the B are in the exact same place on a 2D array (referred to as gameBoard), regardless on where about they meet.

Now I tried to do this by creating two separate integers (called row and column) that increment over the BoardWidth and the BoardHeight (which are constant integers set to 10), but I keep getting the error that "the left hand side of an assignment must be a variable property or indexer."

Now while I believe this error is occurring because of the way that the if statement is written, but I don't know what I can do to change without breaking functionality.

Here's my code:

for (int row = 0; row < BoardWidth; row++)
{
    for (int column = 0; column < BoardHeight; column++)
    {
        if (gameBoard[row, column] == "a" = gameBoard[row, column] == "B")//Where the error is
        {
            //To be written once the error is fixed
        }
    }
}

Upvotes: 0

Views: 2530

Answers (3)

Mischa
Mischa

Reputation: 1333

if (gameBoard[row, column] == "a" = gameBoard[row, column] == "B")

Here you have a = (an assignment) between your two checks.

I think you want to check if the content of gameBoard[row, column] is a or B.
To do this you have to change the = to ||

if (gameBoard[row, column] == "a" || gameBoard[row, column] == "B")

Upvotes: 0

Rahul
Rahul

Reputation: 77896

It because of the = assignment operator in your IF condition as pointed below

if (gameBoard[row, column] == "a" = gameBoard[row, column] == "B")
                                  ^....Here

I think you meant to use a || OR condition like

if (gameBoard[row, column] == "a" || gameBoard[row, column] == "B")

Upvotes: 3

Kyle
Kyle

Reputation: 2018

gameBoard[row, column] == "a" = gameBoard[row, column] == "B" evaluates to true/false = true/false. Since assigning a value to true/false doesn't make sense, you get an error.

Upvotes: 1

Related Questions