kanoonsantikul
kanoonsantikul

Reputation: 199

How can I change enum value inside it scope

This is my enum class

public enum Turn{
    BeginUserTurn,UserTurn,
    BeginEnemyTurn,EnemyTurn;

    public void change(){
        if(this==BeginUserTurn)this=BeginEnemyTurn;
        else this=BeginUserTurn;
    }
}

and I have Turn valiable

Turn turn;
turn=Turn.BeginUserTurn;

I want to change turn value from user to enemy's turn by call

turn.change()

but problem is in enum class,line 6,7 , have an error say

the left-hand side of an assignment must be variable

Upvotes: 1

Views: 168

Answers (1)

khelwood
khelwood

Reputation: 59096

You can't reassign this. It is a keyword, not a variable. Maybe try something like this:

turn = turn.change();

and have change() return the next Turn:

public Turn change() {
    if (this==BeginUserTurn) return BeginEnemyTurn;
    else return BeginUserTurn;
}

Upvotes: 4

Related Questions