Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Type cast a enum to different enum type in the entire List; C#

Type cast a enum to different enum type in the entire List; C#

Scheduled Pinned Locked Moved C#
csharp
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    ptr_Electron
    wrote on last edited by
    #1

    public enum Type
    {
    None = 0,
    Found =1,
    NotAval = 2

    };
    

    public enum ShortType
    {
    None = 0,
    Fd =1,
    NA = 2
    };

    class Properties
    {
    public Type type;
    };

    List props;
    //After that List is populated
    ...
    ...
    //Now I want to type cast the Type enum in the entire list to ShortType with out looping through the entire list
    //Please advice

    //

    L P OriginalGriffO B 4 Replies Last reply
    0
    • P ptr_Electron

      public enum Type
      {
      None = 0,
      Found =1,
      NotAval = 2

      };
      

      public enum ShortType
      {
      None = 0,
      Fd =1,
      NA = 2
      };

      class Properties
      {
      public Type type;
      };

      List props;
      //After that List is populated
      ...
      ...
      //Now I want to type cast the Type enum in the entire list to ShortType with out looping through the entire list
      //Please advice

      //

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Can you avoid that situation in the first place? Cast immediately while populating that list, for example. Or cast when getting the items out of the list.

      ptr_Electron wrote:

      Now I want to type cast the Type enum in the entire list to ShortType with out looping through the entire list

      Not happening. At least, not really. Someone will probably suggest LINQ's Cast<T>[^], but all that does is change a normal loop into a lazy loop.

      P 1 Reply Last reply
      0
      • P ptr_Electron

        public enum Type
        {
        None = 0,
        Found =1,
        NotAval = 2

        };
        

        public enum ShortType
        {
        None = 0,
        Fd =1,
        NA = 2
        };

        class Properties
        {
        public Type type;
        };

        List props;
        //After that List is populated
        ...
        ...
        //Now I want to type cast the Type enum in the entire list to ShortType with out looping through the entire list
        //Please advice

        //

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Go back and fix the problem. If you want long and short names you might be interested in a DescriptionAttribute.

        P 1 Reply Last reply
        0
        • L Lost User

          Can you avoid that situation in the first place? Cast immediately while populating that list, for example. Or cast when getting the items out of the list.

          ptr_Electron wrote:

          Now I want to type cast the Type enum in the entire list to ShortType with out looping through the entire list

          Not happening. At least, not really. Someone will probably suggest LINQ's Cast<T>[^], but all that does is change a normal loop into a lazy loop.

          P Offline
          P Offline
          ptr_Electron
          wrote on last edited by
          #4

          Thanks a lot

          1 Reply Last reply
          0
          • P PIEBALDconsult

            Go back and fix the problem. If you want long and short names you might be interested in a DescriptionAttribute.

            P Offline
            P Offline
            ptr_Electron
            wrote on last edited by
            #5

            Sure, Thanks a lot

            1 Reply Last reply
            0
            • P ptr_Electron

              public enum Type
              {
              None = 0,
              Found =1,
              NotAval = 2

              };
              

              public enum ShortType
              {
              None = 0,
              Fd =1,
              NA = 2
              };

              class Properties
              {
              public Type type;
              };

              List props;
              //After that List is populated
              ...
              ...
              //Now I want to type cast the Type enum in the entire list to ShortType with out looping through the entire list
              //Please advice

              //

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              Even if you could cast a List<T> to a different type, (which you can't without playing silly buggers a lot) you couldn't do what you want: the list is not of the enum, it is a List of the Class containing the enum, which is a very, very different item indeed. Summing you want to do this to have human readable names for your enum elements, have a look at this: Human readable strings for enum elements[^]

              The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              1 Reply Last reply
              0
              • P ptr_Electron

                public enum Type
                {
                None = 0,
                Found =1,
                NotAval = 2

                };
                

                public enum ShortType
                {
                None = 0,
                Fd =1,
                NA = 2
                };

                class Properties
                {
                public Type type;
                };

                List props;
                //After that List is populated
                ...
                ...
                //Now I want to type cast the Type enum in the entire list to ShortType with out looping through the entire list
                //Please advice

                //

                B Offline
                B Offline
                Bernhard Hiller
                wrote on last edited by
                #7

                If the enums are in your sources, try the solutions already suggested. But if you cannot change the sources (e.g. Third Party), you may add a wrapper class - thus actually introducing a third enum which you use in your application, and whenever you need to communicate with Third Party, the wrapper does the translation. Not a nice solution, but feasable.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups