enum questions

  • Thread starter csharpula csharp
  • Start date
C

csharpula csharp

Hello ,
I have 2 questions about enums:

1) How can I recognize defined enum in other projects ,do I have to add
the full namespace path before it such as:

Projects.New Project.Test.MessageType (MessageType is an enum) - each
time I use it or I can somehow do it with using in the head of the file?

2) How can I validate if I got a valid enum value? Is it possible this
way:

if (System.Enum.IsDefined(typeof(MessageType), msgType) == false)

Is it ok?
Thank u!
 
J

Jon Skeet [C# MVP]

csharpula csharp said:
1) How can I recognize defined enum in other projects ,do I have to add
the full namespace path before it such as:

Projects.New Project.Test.MessageType (MessageType is an enum) - each
time I use it or I can somehow do it with using in the head of the file?

Do it with a using directive just as you would for any other type.
2) How can I validate if I got a valid enum value? Is it possible this
way:

if (System.Enum.IsDefined(typeof(MessageType), msgType) == false)

Is it ok?

Exactly - although I'd write it as:

if (!Enum.IsDefined(typeof(MessageType, msgType))

(i.e. have a using directive for the System namespace, and use ! to
negate the result of IsDefined instead of comparing with false.)
 
A

Alfred Myers

Hi,

1) An enum is a type just as others and for that matter you can declare its
namespace in other classes as you do with other types. So declaring
using Projects.New Project.Test;

will enable you to use MessageType without having to specify the fully
qualified name.

2) Yes.

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top