create array of structs, accessed by enum

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

    create array of structs, accessed by enum

    I have an enum. I want a struct of data associated with each one.
    Then, I could use the enum to access the data as needed. In other
    words, I want the enum to represent more than just a unique integer.
    I want it to represent a struct.

    Is there a way to do this nicely?

    Searching seems to say "no."

    (Enum.GetValues (myenum).Length is not a constant, apparantly, which is
    annoying, since I can't use it to *at least* size the array, unless I
    want to create it dynamically, which would solve a portion of this
    problem, but not all.)

    Zytan

  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: create array of structs, accessed by enum

    Zytan,

    You should use a dictionary instead. Even if you could size the array
    properly, you would have to cast the enumeration value to an int every time
    you tried to get an element from the array.

    The dictionary would make it much easier to use.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Zytan" <zytanlithium@g mail.comwrote in message
    news:1175092467 .283330.69860@d 57g2000hsg.goog legroups.com...
    >I have an enum. I want a struct of data associated with each one.
    Then, I could use the enum to access the data as needed. In other
    words, I want the enum to represent more than just a unique integer.
    I want it to represent a struct.
    >
    Is there a way to do this nicely?
    >
    Searching seems to say "no."
    >
    (Enum.GetValues (myenum).Length is not a constant, apparantly, which is
    annoying, since I can't use it to *at least* size the array, unless I
    want to create it dynamically, which would solve a portion of this
    problem, but not all.)
    >
    Zytan
    >

    Comment

    • Zytan

      #3
      Re: create array of structs, accessed by enum

      You should use a dictionary instead. Even if you could size the array
      properly, you would have to cast the enumeration value to an int every time
      you tried to get an element from the array.
      >
      The dictionary would make it much easier to use.
      Ok, thanks, I'll reseach dictionaries and see what they are about.

      Zytan

      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: create array of structs, accessed by enum

        Zytan,

        If you are using .NET 2.0 or above, then use the generic Dictionary<K,
        Vclass in the Sytem.Collectio ns.Generic namespace. If you are using .NET
        1.1, then use the Hashtable class in the System.Collecti ons namespace.


        --
        - Nicholas Paldino [.NET/C# MVP]
        - mvp@spam.guard. caspershouse.co m

        "Zytan" <zytanlithium@g mail.comwrote in message
        news:1175093226 .397737.264090@ y80g2000hsf.goo glegroups.com.. .
        > You should use a dictionary instead. Even if you could size the
        >array
        >properly, you would have to cast the enumeration value to an int every
        >time
        >you tried to get an element from the array.
        >>
        > The dictionary would make it much easier to use.
        >
        Ok, thanks, I'll reseach dictionaries and see what they are about.
        >
        Zytan
        >

        Comment

        • Zytan

          #5
          Re: create array of structs, accessed by enum

          If you are using .NET 2.0 or above, then use the generic Dictionary<K,
          Vclass in the Sytem.Collectio ns.Generic namespace. If you are using .NET
          1.1, then use the Hashtable class in the System.Collecti ons namespace.
          Thanks, I'm using 2.0. Sounds like Dictionary is a HashTable, except
          its strongly typed, and it throws an exception on illegal access.
          Dictionary must be a replacement for HashTable.

          This should be fine.

          Is there any way to initialize a Dictionary in the declaration? I
          don't think so, but it'd be beneficial, since it will essentially be a
          constant in my program.

          Thanks for your help,

          Zytan

          Comment

          • Nicholas Paldino [.NET/C# MVP]

            #6
            Re: create array of structs, accessed by enum

            Zytan,

            Unfortunately, no, there is no way to populate the Dictionary in the
            declaration. C# 3.0 will have a mechanism to do that, but that's a ways
            off.

            And you are right, the Dictionary is really just a strongly-typed hash
            table.


            --
            - Nicholas Paldino [.NET/C# MVP]
            - mvp@spam.guard. caspershouse.co m

            "Zytan" <zytanlithium@g mail.comwrote in message
            news:1175098290 .986846.64710@n 76g2000hsh.goog legroups.com...
            > If you are using .NET 2.0 or above, then use the generic
            >Dictionary<K ,
            >Vclass in the Sytem.Collectio ns.Generic namespace. If you are using
            >.NET
            >1.1, then use the Hashtable class in the System.Collecti ons namespace.
            >
            Thanks, I'm using 2.0. Sounds like Dictionary is a HashTable, except
            its strongly typed, and it throws an exception on illegal access.
            Dictionary must be a replacement for HashTable.
            >
            This should be fine.
            >
            Is there any way to initialize a Dictionary in the declaration? I
            don't think so, but it'd be beneficial, since it will essentially be a
            constant in my program.
            >
            Thanks for your help,
            >
            Zytan
            >

            Comment

            • Zytan

              #7
              Re: create array of structs, accessed by enum

              Unfortunately, no, there is no way to populate the Dictionary in the
              declaration. C# 3.0 will have a mechanism to do that, but that's a ways
              off.
              >
              And you are right, the Dictionary is really just a strongly-typed hash
              table.
              Ok, great, thanks, Nicholas, your help has been wonderful!

              Zytan

              Comment

              Working...