473,385 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Array Indexer Question

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]);
}
}
Nov 15 '05 #1
3 2521
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]);
}
}

Nov 15 '05 #2
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" <gs****@budgetext.com>
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: <#A**************@TK2MSFTNGP12.phx.gbl>
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!TK2MSFTN GP12.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

Nov 15 '05 #3
Rakesh,
Thanks, I knew about indexers but never studying them now I know why and how
to use them.
Thanks again.
grs

"Rakesh Namineni[MSFT]" <ra******@online.microsoft.com> wrote in message
news:np**************@cpmsftngxa06.phx.gbl...
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" <gs****@budgetext.com>
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: <#A**************@TK2MSFTNGP12.phx.gbl>
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!TK2MSFTN GP12.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:196268X-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

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: PhilM | last post by:
I would like some assistance in methodology to reassign/reset array keys to consecutive numbering, while keeping in sync a second array that uses the key of the first as it's key set. If I have...
6
by: ad | last post by:
I have a huge sting array, there are about 1000 element in it. How can I divide the huge array into small ones, and there are one 10 elements in a small one array?
8
by: Rainer Queck | last post by:
Hi NG, how can I implement more then one array property to a class? I have read about the Indexer, but as far as I can see this would only work for a Collection type of class like list or...
6
by: pinetaj | last post by:
Hello, I have a question of using 'property' on accessing elements of array. There is an array member in a class. I'd like to restrict accessing the elements of the array through property. And...
17
by: SemSem | last post by:
i want to know waht is an index and how we use it with a simple example including the main of the program . thanx -- Islam Khalil,
9
by: garyusenet | last post by:
I'm a bit confused about the differences of these two commands (what is the right word for commands here?) when used to enumerate the contents of an array. The below example uses both foreach...
33
by: Zytan | last post by:
I want to make a zero element array. I know that Nothing is not the same as a zero element array, since I can't get the length of, or iterate through, an array = Nothing. I could make a zero...
7
by: hlg | last post by:
I have a question, which must surely have occurred to many programmers since STL first appeared, and yet I have found no reference to it anywhere, suggesting the problem is insoluble. Nevertheless,...
11
by: jacob navia | last post by:
Hi Suppose that I want to create an array of read only items I overload the operator. How can I detect if I am being called within a read context foo = Array; or within a write context...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.