George,
An array is meant to be indexed by an integer, indicating place. You
are using the enumeration (which coincidentally has the same values as what
is kept in the array) to indicate the value of the pieces.
If you want to use an array, you will have to use the cast. Type-safety
is very important in .NET, and making the developer perform the cast is very
important so that the developer knows what is going on.
If you want to get around this, you will have to define your own
collection class and then you can create an indexer which takes an instance
of the type of the enumeration for the indexer value.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
-
mv*@spam.guard.caspershouse.com
"george r smith" <gs****@budgetext.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
I am converting a delphi chess program to C#, this is how I study a new
language and I have the following problem.
How do you use an enumeration value as an indexer for an array ?
This is what I have come up with, but it seems vebose. In Delphi I would
just say
PieceValue[Queen] instead of PieceValue[(int)Piece.Queen].
This is ok if it is the only way but is there a better C# way.
thanks
grs
class Class1
{
enum Piece
{
Queen,
Pawn
};
static void Main()
{
int[] PieceValue = {8,1};
Console.WriteLine(PieceValue[(int)Piece.Queen]);
Console.WriteLine(PieceValue[(int)Piece.Pawn]);
}
}