Visual C++ .NET/Data Type/Enum

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Create an Enumeration

 

#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
enum { ONE = 1, TWO, THREE, FOUR, FIVE = 5, SIX };
int main(void) {
    Console::WriteLine(ONE);
    Console::WriteLine(TWO);
    Console::WriteLine(THREE);
    Console::WriteLine(FIVE);
    Console::WriteLine(SIX);
    return 0;
}


enum class type

 
#include "stdafx.h"
enum class Flavor
{
   Vanilla,
   Chocolate,
   Strawberry
};
int main()
{
   Flavor^ flavor_handle = Flavor::Vanilla;
   Flavor flavor_stack = Flavor::Vanilla;
}


enum format

 
#include "stdafx.h"
using namespace System;

[ Flags ]
enum class Color
{
    Red = 1,
    Blue = 2,
    Green = 4   // use powers of 2
};
int main()
{
   Console::WriteLine("Colors: {0}, {1}, {2}", Color::Red, Color::Blue,Color::Green);
   Console::WriteLine("Colors: {0:d}, {1:d}, {2:d}", Color::Red, Color::Blue,Color::Green);
   Color c = Color::Red | Color::Blue;
   String^ s1 = c.ToString("X"); 
   Console::WriteLine( s1 );

   String^ s2 = Enum::Format( Color::typeid, c , "G");
   Console::WriteLine(s2 );
}


Enum Type

 
#include "stdafx.h"
using namespace System;
enum class PrimeColors { Red, Blue, Yellow };
void main()
{
    PrimeColors color;
    color = PrimeColors::Blue;
    switch (color)
    {
        case PrimeColors::Red :
            Console::WriteLine("Red");
            break;
        case PrimeColors::Blue :
            Console::WriteLine("Blue");
            break;
        case PrimeColors::Yellow :
            Console::WriteLine("Yellow");
            break;
    }
    Console::WriteLine(color.ToString());
}


enum type specified

 
#include "stdafx.h"
using namespace System;
enum class Ordinal : char
{
   zero, one, two, three
};
int main()
{
   char c1 = 3;
   char c2 = 1;
   Ordinal ord1 = safe_cast<Ordinal>(c1);
   Console::WriteLine(ord1.ToString());
}


Specify the hex representation when converting enum to string

 
#include "stdafx.h"
using namespace System;

[ Flags ]
enum class Color
{
    Red = 1,
    Blue = 2,
    Green = 4   // use powers of 2
};
int main()
{
   Console::WriteLine("Colors: {0}, {1}, {2}", Color::Red, Color::Blue,Color::Green);
   Console::WriteLine("Colors: {0:d}, {1:d}, {2:d}", Color::Red, Color::Blue,Color::Green);
   Color c = Color::Red | Color::Blue;
   String^ s1 = c.ToString("X"); 
   Console::WriteLine( s1 );

   String^ s2 = Enum::Format( Color::typeid, c , "G");
   Console::WriteLine(s2 );
}


Use the bitwise OR operator to combine flags

 
#include "stdafx.h"
using namespace System;

[ Flags ]
enum class Color
{
    Red = 1,
    Blue = 2,
    Green = 4   // use powers of 2
};
int main()
{
   Console::WriteLine("Colors: {0}, {1}, {2}", Color::Red, Color::Blue,Color::Green);
   Console::WriteLine("Colors: {0:d}, {1:d}, {2:d}", Color::Red, Color::Blue,Color::Green);
   Color c = Color::Red | Color::Blue;
   String^ s1 = c.ToString("X"); 
   Console::WriteLine( s1 );

   String^ s2 = Enum::Format( Color::typeid, c , "G");
   Console::WriteLine(s2 );
}


Use the Format method of the Enum class

 
#include "stdafx.h"
using namespace System;

[ Flags ]
enum class Color
{
    Red = 1,
    Blue = 2,
    Green = 4   // use powers of 2
};
int main()
{
   Console::WriteLine("Colors: {0}, {1}, {2}", Color::Red, Color::Blue,Color::Green);
   Console::WriteLine("Colors: {0:d}, {1:d}, {2:d}", Color::Red, Color::Blue,Color::Green);
   Color c = Color::Red | Color::Blue;
   String^ s1 = c.ToString("X"); 
   Console::WriteLine( s1 );

   String^ s2 = Enum::Format( Color::typeid, c , "G");
   Console::WriteLine(s2 );
}