473,840 Members | 1,415 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 12274
"Cmtk Software" <Cm**********@n ewsgroup.nospam wrote in message
news:F0******** *************** ***********@mic rosoft.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.publi c.dotnet.langua ges.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::WriteL ine(d == Days::sunday?"I t'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.WriteLi ne(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**********@n ewsgroup.nospam wrote in message
news:F0******** *************** ***********@mic rosoft.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.publi c.dotnet.langua ges.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::WriteL ine(d == Days::sunday?"I t'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.WriteLi ne(d);
d = 0;
ue.Bar(d);
}
}

Willy.

Nov 22 '06 #3
"Cmtk Software" <Cm**********@n ewsgroup.nospam wrote in message
news:29******** *************** ***********@mic rosoft.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
6569
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 specifier is typedef do not declare object." When I compile and run my sample code, the value "2" is displayed. Clearly the typedef has created the enumerators S1, S2, and S3. This appears to contradict K&R2. 2. The program below was created to...
21
4618
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
5396
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
3875
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 to drop the "enum" keyword from my member variable declaration to keep the compiler happy. Shouldn't I technically be able to use the "enum" keyword without a warning?
0
3353
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 question.. The code fails with the error that: "Additional information: COM object that has been separated from its underlying RCW can not be used." if I make a call to pc.Count before iterating though the objects. Dim d As New...
12
6725
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: public enum Days { Sunday };
3
2396
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
8007
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 c++ classes I am building a managed c++ assembly around these classes so that a c# program can use them. The problem is that alot of these c++ classes use enums, I would like to make these visible to the c# application but I cannot find a way to...
2
1475
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 particular feature, and I don't want to port it in an old way. Here is the code below (in java ) that I want to approximate in C#. Note that I have lots of classes that follow this sort of principles. public class Derived extends Base {
0
9856
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9698
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10916
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10598
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9436
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7836
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4071
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3136
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.