MattC
MattC

Reputation: 4004

How to use a C# Enum in C++/CLI

I have a C# assembly containing an enum:

public enum MyEnum
{
    OK = 0, 
    NOT_OK = -1
}

I've added the reference to the C++/CLI project but I don't know how to return a value from a function.

Header:

#pragma once

using namespace System;
using namespace MyNamespace;

namespace NativeWrapper {

    public ref class API
    {
        public:
            static MyNamespace::MyEnum Go();
    };

}

CPP:

#include "StdAfx.h"
#include "API.h"

using namespace NativeWrapper;

MyNamespace::MyEnum API::Go()
{
    return MyEnum.OK;
}

I get the following when I build the project:

error C2275: 'MyNamespace::MyEnum' : illegal use of this type as an expression

Upvotes: 1

Views: 4421

Answers (1)

David Yaw
David Yaw

Reputation: 27864

You need colons, not a period.

MyNamespace::MyEnum API::Go()
{
    return MyEnum::OK;
}

Also, note that if MyEnum was originally written in C++/CLI, you'd need to declare it as public enum class MyEnum { ... } to make it a managed enum that you can use from C#.

Upvotes: 10

Related Questions