Array Indexer Question

  • Thread starter Thread starter george r smith
  • Start date Start date
G

george r smith

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]);
}
}
 
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.
 
Geoge,
The cast is necessarry. But if you want to hide the cast you can make the
PieceValues a struct and try something like this...

struct PieceValues
{
private int[] myValues;

public void Initialize(int[] values) { myValues = values; }

public int this[Piece idx]
{
get { return myValues[(int)idx]; }
set { myValues[(int)idx] = value; }
}
}

Now after you create an instance to PieceValues you can pass the enum value
directly and let the indexer do the job for you. You don't have to have
explicitly worry about the cast everywhere you use PieceValues. Hope this
helps.

--------------------
From: "george r smith" <[email protected]>
Subject: Array Indexer Question
Date: Mon, 3 Nov 2003 08:51:20 -0600
Lines: 29
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <#[email protected]>
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: 216-63-152-112.budgetext.com 216.63.152.112
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:196268
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

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]);
}
}


Rakesh, Visual Studio Enterprise Frameworks and Tools

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
 
Rakesh,
Thanks, I knew about indexers but never studying them now I know why and how
to use them.
Thanks again.
grs

Rakesh Namineni said:
Geoge,
The cast is necessarry. But if you want to hide the cast you can make the
PieceValues a struct and try something like this...

struct PieceValues
{
private int[] myValues;

public void Initialize(int[] values) { myValues = values; }

public int this[Piece idx]
{
get { return myValues[(int)idx]; }
set { myValues[(int)idx] = value; }
}
}

Now after you create an instance to PieceValues you can pass the enum value
directly and let the indexer do the job for you. You don't have to have
explicitly worry about the cast everywhere you use PieceValues. Hope this
helps.

--------------------
From: "george r smith" <[email protected]>
Subject: Array Indexer Question
Date: Mon, 3 Nov 2003 08:51:20 -0600
Lines: 29
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <#[email protected]>
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: 216-63-152-112.budgetext.com 216.63.152.112
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:196268
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

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]);
}
}


Rakesh, Visual Studio Enterprise Frameworks and Tools

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
 
Back
Top