Click here to Skip to main content
15,905,971 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a doubt.
I need to convert managed enum to unmanaged enum.

C#
enum class MANAGED
{
   TYPE1,
   TYPE2
}


C++
enum UNMANAGED
{
   TYPE1,
   TYPE2
}


I did the fillowing code

C++
UNMANAGED unman = (UNMANAGED)((int)man);


But i'm getting undefined value in the variable unman


Please reply....
Thanks in advance..
Posted

Because your using MANAGED enum it will not be converted automatically to int so you should have:
C#
int temp = (int)man; //But "man" must be initialized!  
UNMANAGED unman = (UNMANAGED)temp;
 
Share this answer
 
Assuming the variable 'man is of Type MANAGED, and is assigned, this code will work:
C#
MANAGED man = MANAGED.TYPE1;
UNMANAGED un = (UNMANAGED)((int)man);


An obvious bug is your use of the word 'class here: enum class MANAGED.

This will work:
C#
MANAGED man = MANAGED.TYPE1;
UNMANAGED un = (UNMANAGED) man;
Because each Enum value has an intrinsic numeric value: conversion (to integer) is not required.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900