const Array[TMyThing] of string;

  • Thread starter Thread starter Alexander Muylaert
  • Start date Start date
A

Alexander Muylaert

Hi

In delphi we can create an array of an enum.

type
TMyEnum = (meRed, meGreen, meBlue);

const
cMyArray : array[TMyEnum] of TColor = (clRed, clGreen, clBlue);


This has the huge advantage that the compiler will actually check if enough
elements are inside the array. It is also very easy to get an element.

Color = cMyArray[FEnum];

Is there a way this can be accomplished in C#?

Kind regards

Alexander
 
You can easily comvert Enum to string or value array. Just use string[]
Enum.GetNames(typeof(MyEnum))
or array Enum.GetValues(typeof(MyEnum))

Hope it helps
 
No

I need to define an arry of whatever type that contains as much elements as
their are enumerations. The enumeration is also the index in the table.
This check is performed at compile time. Not at runtime. It is very
usefull for statics.

kind regards

Alexander


Tamir Khason said:
You can easily comvert Enum to string or value array. Just use string[]
Enum.GetNames(typeof(MyEnum))
or array Enum.GetValues(typeof(MyEnum))

Hope it helps
--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "


Alexander Muylaert said:
Hi

In delphi we can create an array of an enum.

type
TMyEnum = (meRed, meGreen, meBlue);

const
cMyArray : array[TMyEnum] of TColor = (clRed, clGreen, clBlue);


This has the huge advantage that the compiler will actually check if enough
elements are inside the array. It is also very easy to get an element.

Color = cMyArray[FEnum];

Is there a way this can be accomplished in C#?

Kind regards

Alexander
 
Alexander Muylaert said:
I need to define an arry of whatever type that contains
as much elements as their are enumerations. The
enumeration is also the index in the table. This check
is performed at compile time. Not at runtime.

I'm not aware of anything like this in C#.

If I wanted to do this, I would write custom get/set functions for the
array that ensure the bounds are valid and part of the enumeration.

P.
 
Back
Top