enum as array index

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gregg Teehan

    #1

    enum as array index

    Suggestions on best practive for array declare / subscript
    using an enum - this is easy in Delphi / Object Pascal.

    I want

    private MyObjectClass [] myObjects;

    myObjects = new MyObjectClass [DateTime.DayOfW eek];
    myObjects [DayOfWeek.Monda y] = new MyObjectClass() ;
    myObjects [DayOfWeek.Tuesd ay] = new MyObjectClass() ;

    but C# says array subscript must be int type

    How best to do this.
    1) what is syntax for cardinality of DayOfWeek enum?
    2) what is 1st and last of enumeration?
    e.g.
    for (DayOfWeek index=low(DayOf Week);
    index <= high(DayOfWeek) ; index++)
    { . . . }

    Gregg






  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: enum as array index

    Gregg,

    There is nothing in the framework for this, so you will have to do it
    yourself.

    You can call the static GetValues method on the Enum type to get the
    values in the enumeration. You can then sort that array. Once you have
    that, you can get the first and last values in the enumeration.

    Hope this helps.

    i
    "Gregg Teehan" <gteehan@nummi. com> wrote in message
    news:032201c39c bb$5c533440$a10 1280a@phx.gbl.. .[color=blue]
    > Suggestions on best practive for array declare / subscript
    > using an enum - this is easy in Delphi / Object Pascal.
    >
    > I want
    >
    > private MyObjectClass [] myObjects;
    >
    > myObjects = new MyObjectClass [DateTime.DayOfW eek];
    > myObjects [DayOfWeek.Monda y] = new MyObjectClass() ;
    > myObjects [DayOfWeek.Tuesd ay] = new MyObjectClass() ;
    >
    > but C# says array subscript must be int type
    >
    > How best to do this.
    > 1) what is syntax for cardinality of DayOfWeek enum?
    > 2) what is 1st and last of enumeration?
    > e.g.
    > for (DayOfWeek index=low(DayOf Week);
    > index <= high(DayOfWeek) ; index++)
    > { . . . }
    >
    > Gregg
    >
    >
    >
    >
    >
    >[/color]


    Comment

    • Jay B. Harlow [MVP - Outlook]

      #3
      Re: enum as array index

      Gregg,
      In addition to Nicholas's comments.

      When I want a collection keyed by an Enum, I consider using a Hashtable
      instead of an Array.

      This allows me to use the Enum itself for the key.

      Of course the Hashtable will be more overhead then an Array (or ArrayList),
      which I take into consideration.

      Truthfully I normally create type safe collections with either
      CollectionBase or DictionaryBase in System.Collecti ons. In the case of an
      enum index/key, I define the parameter to the indexer, as the enum.

      I think when we get to .NET 2.0 (Whidbey, aka VS.NET 2004) we will have a
      couple of better options available in terms of Generics! :-))

      Hope this helps
      Jay

      "Gregg Teehan" <gteehan@nummi. com> wrote in message
      news:032201c39c bb$5c533440$a10 1280a@phx.gbl.. .[color=blue]
      > Suggestions on best practive for array declare / subscript
      > using an enum - this is easy in Delphi / Object Pascal.
      >
      > I want
      >
      > private MyObjectClass [] myObjects;
      >
      > myObjects = new MyObjectClass [DateTime.DayOfW eek];
      > myObjects [DayOfWeek.Monda y] = new MyObjectClass() ;
      > myObjects [DayOfWeek.Tuesd ay] = new MyObjectClass() ;
      >
      > but C# says array subscript must be int type
      >
      > How best to do this.
      > 1) what is syntax for cardinality of DayOfWeek enum?
      > 2) what is 1st and last of enumeration?
      > e.g.
      > for (DayOfWeek index=low(DayOf Week);
      > index <= high(DayOfWeek) ; index++)
      > { . . . }
      >
      > Gregg
      >
      >
      >
      >
      >
      >[/color]


      Comment

      Working...