: BCE0051: Operator '==' cannot be used with a left hand side of type 'Anenumname'

Hi,

: BCE0051: Operator ‘==’ cannot be used with a left hand side of type ‘Anenumname’ and a right hand side of type ‘System.Object’.

for the code switch(ScriptName.Anenumname)

How do we remove this error???

Regards,
vanhouten777.

public enum MyEnum {
	A,
	B,
	C
}

var x = MyEnum.A;

if (x == MyEnum.A) {
	// compiles
}

if (x == MyEnum.B) {
	// compiles
}

if (x == 42) {
	// does not compile, the thing on the right of the == needs to be a MyEnum, not an int, string, object or any other type
}

switch (x) {
case MyEnum.A:
	// compiles
	break;
case MyEnum.B:
	// compiles
	break;
case 42:
	// does not compile, the case value needs to be a MyEnum, not an int, string, object or any other type
	break;
}

You’d get more assistance if you post a snippet of the code with your inquiry.

You are currently trying to compare apples and oranges (Or in this case ‘Objects’ and ‘Anenumnames’).
Make sure the method you are referring to returns an ‘Object’ type.

Hi Smooth P and mafiadude1 for replying.
Regards,
vanhouten777.

You need to type cast the integer as the MyEnum.
if (dummy == (MyEnum)2)
{
//Now Compiles :slight_smile:
}
switch (dummy) {
case MyEnum.A:
// compiles
break;
case MyEnum.B:
// compiles
break;
case (MyEnum)42:
// Now Compiles
break;
}

Also can specify values for your enum values

public enum MyEnum {
A=1,
B=2,
C=3
}
I don’t believe they get ISO values if you don’t do that. And even if they do, don’t check for that.