Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

enum questions

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
csharpula csharp - 02 Mar 2008 10:40 GMT
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!
Jon Skeet [C# MVP] - 02 Mar 2008 13:50 GMT
> 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.)

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Alfred Myers - 02 Mar 2008 14:23 GMT
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

> Hello ,
> I have 2 questions about enums:
[quoted text clipped - 14 lines]
>
> *** Sent via Developersdex http://www.developersdex.com ***

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.