1

I have the following statement:

private Enum _statusValue;

What I'd really like to say is:

private Enum _statusValue = 0;

even though I know it's redundant. It's just that I like to explicitly specify the initialization.

But that is not allowed.

Is there some simple way of specifying the initialization on this kind of declaration?

EDIT - Let me try again.

Here is a contrived / simplified example of what I'm doing.

using System;

namespace EnumTesting
{
   public enum EFixedPhoneUsability
   {
      UnknownValue,       // Should not occur?
      IsUsable,           // User is near the telephone
      NotUsable,          // User not near the telephone
      BeingUsed,          // Busy
      DoNotDisturb,       // This is phone physical state, not user attitude
      CallsForwarded      // This is phone physical state
   }

   public enum EMobilePhoneConnectivity
   {
      UnknownValue,       // Should not occur?
      OnNetIdle,          // I.e., usable
      OnNetBusy,          // Being used
      NoConnection        // Turned off, in elevator or tunnel or far from civilization
   }

   public class Program
   {
      public Enum StatusValue;

      static void Main(string[] args)
      {
         Program p = new Program();

         p.StatusValue = EMobilePhoneConnectivity.NoConnection;

         int i = (int) (EMobilePhoneConnectivity) p.StatusValue;

         p.StatusValue = EFixedPhoneUsability.DoNotDisturb;

         i = (int) (EFixedPhoneUsability) p.StatusValue;
      }
   }
}

So my question is, can I add a generic initializer to this statement?

      public Enum StatusValue;

SECOND EDIT:

Never mind, I have discovered the error of my ways, thanks to this posting:

How to convert from System.Enum to base integer?

The key phrase, which made me realize what I was doing wrong, is this: "enumerations are value types (and internally represented only by integer constants) while System.Enum is a reference type".

So I do not want to say this:

private Enum _statusValue = 0;

I want to say this, which is perfectly valid:

private Enum _statusValue = null;

Thank you to those who tried to help.

3
  • private Enum _statusValue is a type definition. You can't initialize a type definition. Commented Dec 10, 2010 at 16:29
  • I don't think that is correct. Isn't it a field definition? It works like a field definition - I can assign a value to it using some other enum, and retrieve the value from it using a cast. Commented Dec 10, 2010 at 17:05
  • @RenniePet: you can answer your own question, and then accept it. That way this question gets closed. Commented Jul 11, 2011 at 6:14

2 Answers 2

10

You could always declare your Enum slightly differently and add a default state (which is actually suggested by Microsoft):

public enum MyEnum
{
    Default = 0,
    One = 1, 
    Two = 2
}

Or simply (automatic numbering starts at 0):

public enum MyEnum
{
    Default,
    One,
    Two
}

Which would allow you to do the following:

private MyEnum _enum = MyEnum.Default;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your suggestion, but what I'm hoping to do is to avoid introducing a new enum MyEnum whose only purpose would be to provide syntactic sugar for not being able to use a literal zero. I've edited and expanded my question, and I'd appreciate it if you'd take a look at it again. Thanks.
2

My question was basically in error. I had mistakenly thought Enum (like enums) was a value type, but it is actually a reference type.

So I do not want to say this:

private Enum _statusValue = 0; 

I want to say this, which is perfectly valid:

private Enum _statusValue = null; 

Thank you to those who tried to help.

Comments

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.