
Getting back int value from Enum?
Soren,
Quote:
> __value enum Test { ONE=1, FIVE=5, SIX=6 };
> Array* pTest = System::Enum::GetValues(__typeof(Test));
> int __gc* pInts = new __gc[pTest->Length];
> // Store actual int values, and display the string version of the enum
> values
> for (int i=0; i<pTest ->Length; i++)
> {
> // Display string version
> Console::WriteLine(pArr->GetValue(i));
> // Store actual int value
> pInts[i] = <how to get the int value??>
> }
Simple casting. GetValue() will return the actual value, but with the Enum
type. Just cast that to int to get the integral value. Consider this simple
example:
#using <mscorlib.dll>
using namespace System;
__value enum Test { ONE=1, FIVE=5, SIX=6 };
int main()
{
Test values[] = dynamic_cast<Test[]>(Enum::GetValues(__typeof(Test)));
for ( int i=0; i < values->Length; i++ ) {
Console::WriteLine(S"{0}={1}", __box(values[i]), __box((int)values[i]));
Quote:
}
}
--
Tomas Restrepo