472,958 Members | 2,555 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 12210
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Martin | last post by:
This post asks two questions, which I've illustrated in one C source file (see below), which clean compiles on my GNU compiler. 1. In K&R2, Section A8.9 it says "Declarations whose storage class...
21
by: Andreas Huber | last post by:
Hi there Spending half an hour searching through the archive I haven't found a rationale for the following behavior. using System; // note the missing Flags attribute enum Color {
2
by: babylon | last post by:
I have an enum public enum MyEnum : int { X, Y } I have to do int a = (int) MyEnum.X; can i overload the operator or other means to do something like
5
by: Alan Cobb | last post by:
Hi, In the managed C++ class below I get compile warning C4677 from VS2003. "signature of non-private function contains assembly private type", even though the managed enum is public. I have...
0
by: Ewart MacLucas | last post by:
generated some WMI managed classes using the downloadable extensions for vs2003 from mircrosoft downloads; wrote some test code to enumerate the physicall processors and it works a treat, but a...
12
by: Cmtk Software | last post by:
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: ...
3
by: =?Utf-8?B?Sm9hY2hpbQ==?= | last post by:
I'm trying to use an enum created in Managed c++ in a C# project. I can reach the enums name, i.e. namespace MyNS { public enum MyEnum { EOne, ETwo };
3
by: Dean Mitchell | last post by:
Hi everyone, We have a c++ server application that we are writing a GUI client application for. To save our time and to avoid duplicating all the code and functionality that already exists in...
2
by: puzzlecracker | last post by:
I am porting old java code to csharp and now facing a stumbling block. Before advent of enum in Java, developers used enum-like structures, shown below. However, AFAIK, CSharp isn't lacking this...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.