Array Indexer Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • george r smith

    #1

    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.Quee n].

    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.WriteLi ne(PieceValue[(int)Piece.Quee n]);
    Console.WriteLi ne(PieceValue[(int)Piece.Pawn]);
    }
    }


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Array Indexer Question

    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]
    - mvp@spam.guard. caspershouse.co m

    "george r smith" <gsmith@budgete xt.com> wrote in message
    news:%23APmynho DHA.1488@TK2MSF TNGP12.phx.gbl. ..[color=blue]
    > 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.Quee n].
    >
    > 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.WriteLi ne(PieceValue[(int)Piece.Quee n]);
    > Console.WriteLi ne(PieceValue[(int)Piece.Pawn]);
    > }
    > }
    >
    >[/color]


    Comment

    • Rakesh Namineni[MSFT]

      #3
      RE: Array Indexer Question

      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.

      --------------------[color=blue]
      >From: "george r smith" <gsmith@budgete xt.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: <#APmynhoDHA.14 88@TK2MSFTNGP12 .phx.gbl>
      >Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
      >NNTP-Posting-Host: 216-63-152-112.budgetext.c om 216.63.152.112
      >Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP12.phx.g bl
      >Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1962 68
      >X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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.Quee n].
      >
      >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.WriteLi ne(PieceValue[(int)Piece.Quee n]);
      > Console.WriteLi ne(PieceValue[(int)Piece.Pawn]);
      > }
      >}
      >
      >
      >[/color]


      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

      Comment

      • george r smith

        #4
        Re: Array Indexer Question

        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]" <rakeshna@onlin e.microsoft.com > wrote in message
        news:nptUDjkoDH A.1804@cpmsftng xa06.phx.gbl...[color=blue]
        > 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[/color]
        value[color=blue]
        > 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.
        >
        > --------------------[color=green]
        > >From: "george r smith" <gsmith@budgete xt.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: <#APmynhoDHA.14 88@TK2MSFTNGP12 .phx.gbl>
        > >Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
        > >NNTP-Posting-Host: 216-63-152-112.budgetext.c om 216.63.152.112
        > >Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP12.phx.g bl
        > >Xref: cpmsftngxa06.ph x.gbl[/color][/color]
        microsoft.publi c.dotnet.langua ges.csharp:1962 68[color=blue][color=green]
        > >X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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.Quee n].
        > >
        > >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.WriteLi ne(PieceValue[(int)Piece.Quee n]);
        > > Console.WriteLi ne(PieceValue[(int)Piece.Pawn]);
        > > }
        > >}
        > >
        > >
        > >[/color]
        >
        >
        > Rakesh, Visual Studio Enterprise Frameworks and Tools
        >
        > This posting is provided "AS IS" with no warranties, and confers no[/color]
        rights.[color=blue]
        > Use of included script samples are subject to the terms specified at
        > http://www.microsoft.com/info/cpyright.htm
        >[/color]


        Comment

        Working...