471,122 Members | 952 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,122 software developers and data experts.

C++/CLI managed Enum appears empty in CSharp

I'm trying to define an enum which will be used from unmanaged c++, C++/CLI
managed c++ and from C#.
I defined the following enum in a VS dll project set to be compiled with the
/clr switch:

public enum Days
{
Sunday
};

After building the project, I added the created DLL as a reference in a C#
project.
Trying to use this enum from inside the C# project, Days itself was visible
but none of its fields (Sunday) existed (Days was empty).
For verification, I right clicked on the DLL reference and selected "View in
Object Browser". Again, "Days" existed but was empty - Sunday simply wasn't
there.
I tried creating an enum class in the CLI managed c++ project:
public enum class Days
{
Sunday
};

And both the enum itself and its fields were visible and usable from C#.
However this cannot be my solution because the "enum class" cannot be used
from unmanaged c++.

The enum class reference on MSDN
(http://msdn2.microsoft.com/en-us/lib...49(VS.80).aspx) states:
"A named, standard enum compiled with /clr will be visible in the assembly
as a managed enum, and can be consumed by any other managed compiler."

How can I make Sunday accessible from C#?

Thanks in advance.

Nov 22 '06 #1
3 12082
"Cmtk Software" <Cm**********@newsgroup.nospamwrote in message
news:F0**********************************@microsof t.com...
I'm trying to define an enum which will be used from unmanaged c++, C++/CLI
managed c++ and from C#.
I defined the following enum in a VS dll project set to be compiled with the
/clr switch:

public enum Days
{
Sunday
};

After building the project, I added the created DLL as a reference in a C#
project.
Trying to use this enum from inside the C# project, Days itself was visible
but none of its fields (Sunday) existed (Days was empty).
For verification, I right clicked on the DLL reference and selected "View in
Object Browser". Again, "Days" existed but was empty - Sunday simply wasn't
there.
I tried creating an enum class in the CLI managed c++ project:
public enum class Days
{
Sunday
};

And both the enum itself and its fields were visible and usable from C#.
However this cannot be my solution because the "enum class" cannot be used
from unmanaged c++.

The enum class reference on MSDN
(http://msdn2.microsoft.com/en-us/lib...49(VS.80).aspx) states:
"A named, standard enum compiled with /clr will be visible in the assembly
as a managed enum, and can be consumed by any other managed compiler."

How can I make Sunday accessible from C#?

Thanks in advance.

Please post C++ related questions to the C++ NG at microsoft.public.dotnet.languages.VC.
That said, you are confusing the semantics of both managed and unmanaged enums, the
unmanaged enum will be visible to the managed consumer but it keeps the semantics of an
unmanaged enum.
Following illustrates what's meant with this:

//CPP file compile with /clr
#using <System.dll>
using namespace System;
public enum Days {
sunday,
monday,
};

public ref class UseEnum {
public:
Days Foo()
{
Days day = monday;
return day;
}
void Bar(Days d)
{
Console::WriteLine(d == Days::sunday?"It's sunday":"Eek it's monday");
}
};

Note that compiling above will produce a "non-standard extention used " warning for
Days::sunday!

//C# consuming native C enum
using System;
public class Application
{
static void Main()
{
UseEnum ue = new UseEnum();
Days d = ue.Foo();
Console.WriteLine(d);
d = 0;
ue.Bar(d);
}
}

Willy.

Nov 22 '06 #2
Hi Willy,

I've posted here because there's no problem in cosuming the enum from c++.
the only problem is when i'm trying to use this enum from C#.

Regarding your solution: I wanted to know if I can use the CLI managed
standard enum from C# just like I'd have used any other C# defined enum.
In your C# code, in the penultimate line you wrote: "d = 0;"
This's exactly what i'm trying to avoid. I want to be able to use this enum
as normal and write: "d = Sunday;" Or "d = Days.Sunday;".

Do you know of a way to do so?
"Willy Denoyette [MVP]" wrote:
"Cmtk Software" <Cm**********@newsgroup.nospamwrote in message
news:F0**********************************@microsof t.com...
I'm trying to define an enum which will be used from unmanaged c++, C++/CLI
managed c++ and from C#.
I defined the following enum in a VS dll project set to be compiled with the
/clr switch:

public enum Days
{
Sunday
};

After building the project, I added the created DLL as a reference in a C#
project.
Trying to use this enum from inside the C# project, Days itself was visible
but none of its fields (Sunday) existed (Days was empty).
For verification, I right clicked on the DLL reference and selected "View in
Object Browser". Again, "Days" existed but was empty - Sunday simply wasn't
there.
I tried creating an enum class in the CLI managed c++ project:
public enum class Days
{
Sunday
};

And both the enum itself and its fields were visible and usable from C#.
However this cannot be my solution because the "enum class" cannot be used
from unmanaged c++.

The enum class reference on MSDN
(http://msdn2.microsoft.com/en-us/lib...49(VS.80).aspx) states:
"A named, standard enum compiled with /clr will be visible in the assembly
as a managed enum, and can be consumed by any other managed compiler."

How can I make Sunday accessible from C#?

Thanks in advance.


Please post C++ related questions to the C++ NG at microsoft.public.dotnet.languages.VC.
That said, you are confusing the semantics of both managed and unmanaged enums, the
unmanaged enum will be visible to the managed consumer but it keeps the semantics of an
unmanaged enum.
Following illustrates what's meant with this:

//CPP file compile with /clr
#using <System.dll>
using namespace System;
public enum Days {
sunday,
monday,
};

public ref class UseEnum {
public:
Days Foo()
{
Days day = monday;
return day;
}
void Bar(Days d)
{
Console::WriteLine(d == Days::sunday?"It's sunday":"Eek it's monday");
}
};

Note that compiling above will produce a "non-standard extention used " warning for
Days::sunday!

//C# consuming native C enum
using System;
public class Application
{
static void Main()
{
UseEnum ue = new UseEnum();
Days d = ue.Foo();
Console.WriteLine(d);
d = 0;
ue.Bar(d);
}
}

Willy.

Nov 22 '06 #3
"Cmtk Software" <Cm**********@newsgroup.nospamwrote in message
news:29**********************************@microsof t.com...
Hi Willy,

I've posted here because there's no problem in cosuming the enum from c++.
the only problem is when i'm trying to use this enum from C#.

Regarding your solution: I wanted to know if I can use the CLI managed
standard enum from C# just like I'd have used any other C# defined enum.
In your C# code, in the penultimate line you wrote: "d = 0;"
This's exactly what i'm trying to avoid. I want to be able to use this enum
as normal and write: "d = Sunday;" Or "d = Days.Sunday;".

Do you know of a way to do so?
The C++ compiler does expose the native enum as a managed enum class, that's all, it exposes
it's undelying type, but it doesn't produce the metadata for it's enumerator names. So what
you get is access to it's underlying type (an int by default) through the enum name (see:
same sematics in the doc's).
The same is true for C++, try to consume a native enum defined in a external assembly, it
won't work either. Don't be fooled by the samples in msdn, both consumer and native enum
definition are in the same compiland (same assembly).

Willy.

Nov 22 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Martin | last post: by
21 posts views Thread by Andreas Huber | last post: by
reply views Thread by Ewart MacLucas | last post: by
12 posts views Thread by Cmtk Software | last post: by
3 posts views Thread by =?Utf-8?B?Sm9hY2hpbQ==?= | last post: by
3 posts views Thread by Dean Mitchell | last post: by
2 posts views Thread by puzzlecracker | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.