Adding native enum values to an ArrayList

G

Guest

Hi All,

How do I add native enum values to an ArrayList?

Let's say I have a native enum:

typedef enum _EVENT_CODE
{
A0 = 0,
A1,
A2,
// ......
Z9
} EVENT_CODE, *PEVENT_CODE;

and a value of this type:

EVENT_CODE code = A0;

In order to store an value in ArrayList, I have to convert it to a
System::Object type pointer. Is there a way to do that? I mean, if there is a
way to convert a native enum value to one of any other type, the job will be
done, right?

The last thing I want to do is to write a switch statement; map each of the
enum value to an integer and store the integers in ArrayList instead.

Many thanks!
 
M

Mark Salsbery [MVP]

SeeSharp said:
Hi All,

How do I add native enum values to an ArrayList?

Let's say I have a native enum:

typedef enum _EVENT_CODE
{
A0 = 0,
A1,
A2,
// ......
Z9
} EVENT_CODE, *PEVENT_CODE;

and a value of this type:

EVENT_CODE code = A0;

In order to store an value in ArrayList, I have to convert it to a
System::Object type pointer. Is there a way to do that? I mean, if there
is a
way to convert a native enum value to one of any other type, the job will
be
done, right?

The last thing I want to do is to write a switch statement; map each of
the
enum value to an integer and store the integers in ArrayList instead.

Many thanks!


You could store the enums as ints, something like this (borrowing from SDK
sample code to save typing):

ArrayList^ myAL = gcnew ArrayList;
//myAL->Add( safe_cast<Object^>((int)A0) );
myAL->Add( (int)A0 );
myAL->Add( (int)A1 );
myAL->Add( (int)A2 );
myAL->Add( (int)A3 );
myAL->Add( (int)A4 );

Console::WriteLine( "myAL" );
Console::WriteLine( " Count: {0}", myAL->Count );
Console::WriteLine( " Capacity: {0}", myAL->Capacity );
Console::Write( " Values:" );

IEnumerator^ myEnum = myAL->GetEnumerator();
while ( myEnum->MoveNext() )
{
int n = (int)myEnum->Current;
Console::Write( " {0}", n );
//EVENT_CODE ec = (EVENT_CODE)n;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top