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. 3 12258
"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.
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.
"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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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
{
|
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
|
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?
|
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...
| |
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
};
|
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
};
|
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...
|
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 {
|
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...
|
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,...
| |
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...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |