2

I would like to create my enums as nullable rather than add a default entry with a default of 0.

However, in the following scenario I get syntax errors and I can't understand why, or how to fix it at the moment. It's probably something simple, but the simplest things...

Here's my properties that decare enums as nullable:

public Gender? Gender { get; private set; }

public MaritalStatus? MaritalStatus { get; private set; }

Here's a method that is giving me the syntax error, which is Gender does not contain..., MaritalStatus does not contain...:

    string GetTitle()
    {
        if (Gender == null || MaritalStatus == null)
            return null;
        if (Gender == Gender.M) // Error here!
            return "Mr";
        else if (
            MaritalStatus == MaritalStatus.Married ||
            MaritalStatus == MaritalStatus.Separated ||
            MaritalStatus == MaritalStatus.Widowed) // Error here!
            return "Mrs";
        else
            return "Ms";
    }

Any advice appreciated.

2
  • try specifying this.Gender == Gender.M Commented Nov 11, 2015 at 11:39
  • Almost Ned - it wasn't enough to add this but this worked this.Gender == DataLists.Gender.M Commented Nov 11, 2015 at 11:43

1 Answer 1

4

You have enums MaritalStatus and Gender. At the same time, you have properties named MaritalStatus and Gender. You need to avoid this.

Here:

if (Gender == Gender.M)
if (MaritalStatus == MaritalStatus.Married)

the syntax is incorrect since Gender and MaritalStatus are recognized as variables, but not as type.
Moreover, you need to use .Value to access a value of Nullable.

So, you can explicitly specify the namespace:

if (Gender.Value == YourNamespace.Gender.M)
if (MaritalStatus.Value == YourNamespace.MaritalStatus.Married)

but I strongly recommend to rename your enums to GenderEnum and MaritalStatusEnum.

Why does it happen?

This problem is simply reproducable here:

enum SameName { Value }
class Tester
{
   void Method1() {
      SameName SameName;
      SameName test = SameName.Value; // Works!
   }
   void Method2() {
      string SameName;
      SameName test = SameName.Value; // Doesn't work! Expects string method Value
   }
}

In this answer Eric Lippert has described the reason of this:

C# was designed to be robust in the face of a property named the same as its type because this is common:

class Shape
{
    public Color Color { get; set; }
    ...

If you have a type Color, it is very common to have a property also called Color, and there's no good way to rename either of them. Therefore C# was designed to handle this situation reasonably elegantly.

So, if your variable is of type Enum, then it refers to the enum member; else - it refers to variable. Enum? belongs to "else".

Sign up to request clarification or add additional context in comments.

3 Comments

Can you explain why the above works when the property type is not nullable enum, but just enum?
@IvanStoev I have tried to give an explanation, but simply haven't found it. It looks like it recognizes that Gender. will refer to enum type. However, in case it is not a Enum (including Enum?), it refers to a variable. It even doesn't work for int. Look: works and doesn't. I would appreciate if someone else gives this explanations :)
Yeah, I know. Just a quite annoying that adding a single ? in the property definition breaks the existing code inside the class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.