sriesch

Is it possible to use an enum inside a managed class

For example, I put the following line of code inside my MySettings class under a private section:

enum OptionFileIndexes {OptionIndexLogPathAndFilename, LastOptionIndexCount};

Then I add the following line in a method of the MySettings class:

array<System:Tongue Tiedtring^>^ FileContents=gcnew array<System:Tongue Tiedtring^>(OptionFileIndexes::LastOptionIndexCount);

Unfortunately, compiling generates an error "Cannot define an unmanaged enum 'MySettings:SurpriseptionFileIndexes' inside managed 'MySettings'". If instead of just using "enum" I use "class enum", I can avoid this error but then I need to modify the array declaration to include a static_cast, like "array<System:Tongue Tiedtring^>^ FileContents=gcnew array<System:Tongue Tiedtring^>(static_cast<int>(OptionFileIndexes::LastOptionIndexCount));", which is really clunky and hard to read (not sure if it might introduce other problems I'm not thinking of as well )


Re: Visual C++ Language Declaring enum inside managed class

Nishant Sivakumar

To declare a managed enum use the following syntax :-

Code Snippet

enum class YourEnum

{

...

};






Re: Visual C++ Language Declaring enum inside managed class

Ben Voigt

The cast won't introduce any problems, but yes it is less readable. Native enums have an implicit conversion to int, but for .NET enums the conversion is explicit.

Also, you should probably use safe_cast in managed code.




Re: Visual C++ Language Declaring enum inside managed class

sriesch

OK, thanks! (and yes, I meant "enum class", not "class enum", typo).