473,395 Members | 1,613 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 12238
"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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.