Operand ' ==' cannot be applied to operands of type (struct)

2.2k views Asked by At
public class Human
{
    public setGender Gender { get; set; }

    public void setHeight(Human Person)
    {
        if (Person.Gender == setGender.Male) // <-- This is where the error is.
        {

        }
    }
}

public struct setGender
{
    public static setGender Male { get; set; }
    public static setGender Female { get; set; }
}

When I put in the preceding code, I get this error saying,

operand '==' cannot be applied to operands of type 'Life.setGender' and 'Life.setGender'

(The namespace is "Life" by the way. That is why it is 'Life.setGender')

I had tried to look it up but I still don't know what that means. Could someone please help me with error.

I am using C#.

3

There are 3 answers

0
Andrew T Finnell On BEST ANSWER

I am pretty sure what you really want is this:

public enum Gender 
{
    Male, Female
}

public class Human
{
    public Gender Gender { get; set; }

    public void setHeight(Human person)
    {
        if (person.Gender == Gender.Male)
        {

        }
    }
}
0
Joey On

You're trying to access an instance property on the class. You may want Person.Gender == Gender.Male in this case.

Also note that your naming is a little off. In C# types use PascalCase, as do properties and methods. Fields and locals use camelCase.

2
Alex On

You must override the == operator. Check this link on MSDN: http://msdn.microsoft.com/en-us/library/dd183755.aspx

However I do not see what can be accomplished with this code. Enums would be appropriate to use in this case, not structs.