locked
HOWTO: Define managed Enum in C++.NET RRS feed

  • Question

  •  

    Hi all,

     

    I'm developing class library with C++.NET. I'm defining my enum as follows:

     

    Code Block

    namespace myNameSpace

    {

    //Enumeration for all Return Codes

    public enum class ReturnCodes

    {

    FUNC_OK,

    FUNC_FAIL,

    ........

    };

     

    public ref class myAPI

    {

    public:

    ReturnCode^ FUNC1 ();

    ReturnCode^ FUNC2 ();

    };}

     
    These function are compiled to class library.
    Then I'm trying to call these functions from C# code:
     
    Code Block
    usign myNameSpace;
     
    namespace CshNameSpace
    {
    public class User
    {
    public void CallF1()
    {
    myAPI api = new myAPI();
    ReturnCodes rc = api.FUNC1();
    .....
    }
    }
    }

     

     

    I'm receiving compilation error:

    Cannot implicitly convert type 'System.Enum' to 'myNameSpace.ReturnCode'. An explicit conversion exists (are you missing a cast?)

     

    Compiler recognizes that type of value returned by FUNC1() is System.Enum, while it should be myNameSpace.ReturnCode.

     

    What is wrong?

     

    Thanks in advance,

    Michael.

    Sunday, November 11, 2007 2:27 PM

Answers

  • Change the C++/CLI function signature to:

    ReturnCode FUNC1 ();  //i.e., remove the 'hat'

     

    The 'hat' is not generally used for value types.  Both 'enum class' and 'enum struct' are value types in C++/CLI.

     

    Sunday, November 11, 2007 3:35 PM

All replies

  • Change the C++/CLI function signature to:

    ReturnCode FUNC1 ();  //i.e., remove the 'hat'

     

    The 'hat' is not generally used for value types.  Both 'enum class' and 'enum struct' are value types in C++/CLI.

     

    Sunday, November 11, 2007 3:35 PM
  • Thanks!

    It has solved my issue!

     

    Sunday, November 11, 2007 5:30 PM