Visual C++ .NET/Data Type/Enum
Содержание
Create an Enumeration
<source lang="csharp">
- 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;
}
</source>
enum class type
<source lang="csharp">
- include "stdafx.h"
enum class Flavor {
Vanilla, Chocolate, Strawberry
}; int main() {
Flavor^ flavor_handle = Flavor::Vanilla; Flavor flavor_stack = Flavor::Vanilla;
}
</source>
enum format
<source lang="csharp">
- 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 );
}
</source>
Enum Type
<source lang="csharp">
- 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());
}
</source>
enum type specified
<source lang="csharp">
- 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());
}
</source>
Specify the hex representation when converting enum to string
<source lang="csharp">
- 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 );
}
</source>
Use the bitwise OR operator to combine flags
<source lang="csharp">
- 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 );
}
</source>
Use the Format method of the Enum class
<source lang="csharp">
- 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 );
}
</source>