I am trying my first attempts with C++/CLI but I have a minor problem.
I am building a wrapper usable by all .NET languages which is importing unmanaged C/C++ code and is linking with a custom library file.
I am creating a ref class as:
public ref class CMyTest
{
public:
int Init()
{
...call old style function
}
}
This function returns an integer. This integer is encoded as a old style typedef'd enum:
typedef enum _errors
{
..
} Errors;
I would like the client code to look like this:
CMyTest ^test = gcnew CMyTest;
if (test->Init() != CMyTest.EnumError)
...
Is there any way to get this old style enum to become a type inside my wrapper class This should preferedly be done without retyping all the values.
Thanks in advance.

Importing enum under C++/CLI
ChuaWenChing
Well I was thinking about accessing the Error class from C# in the same manner as you showed from C++ like
if (somethin != CTest.Error.error1)
without having to make objects of the Error class. But I think from your explanation it should be ok.
Marmot74
For example:
enum Errors {
error1,
error2,
error3
};
becomes:
ref class MyTest {
public:
enum class Errors {
error1,
error2,
error3
};
public:
void Init();
};
void f(MyTest^ myTest) {
if (myTest->Init() == MyTest::Errors::error1) {
// ...
}
}
One thing to note is that CLR enums are scoped (the members of the enum are not injected into the enclosing scope as they are in Standard C++ (and C)) so you need to explicitly include the name of the enum in the qualified name.
Simon Masters
I wonder: Would it be possible to create a 'enum class' and then make another "main" class which extends from it The purpose would be to get them under same class.
Another question is what about the 'enum class' and 'static' keyword Isn't it needed somewhere
The overall idea is to make a class library and use it from VB.NET and C# afterwards so anything I need to be aware of is greatly apreciated. :-)
Thanks in advance.
Brett H
No it is not possible to extend an enum: in CLR-speak they are value types - they "inherit" from System::Enum which in turn inherits from System::ValueType - so they are implicitly sealed and cannot be extended.
I'm not sure what you mean about "enum class" and "static" - do you mean the C# class modifier "static" If you do then this is unnecessary - the C# class modifier "static" maps to "abstract sealed" in C++ and it is not required in this case. As enums are value-types they are implicitly "sealed" and they are not "abstract" as you can create instances of them.