none
How to iterate all members of type-safe enum design pattern by foreach()? RRS feed

  • Question

  • Dear All,

    How to iterate all members of type-safe enum design pattern by foreach()?

    The expected should be 

    Branch.Apple.EN;

    Branch.Banana.EN;

    Branch.Cherry.EN;

        public sealed class Branch
        {
            public static Branch Apple { get { return new Branch(1, "蘋果", "Apple"); } }
            public static Branch Banana { get { return new Branch(2, "香蕉", "Banana"); } }
            public static Branch Cherry { get { return new Branch(3, "櫻桃", "Cherry"); } }
    
            public readonly int Number;
            public readonly string TW;
            public readonly string EN;
    
            private Branch(int Number, string TW, string EN)
            {
                this.Number = Number;
                this.TW = TW;
                this.EN = EN;
            }
        }

    Thanks and best regards,

    E-John

    Friday, May 18, 2018 2:00 PM

Answers

  • The next loop will work:

       foreach( var b in Branch.Values )

       {

          // . . .

       }

     

    if you add the next member to the Branch class:

       public static IEnumerable<Branch> Values

       {

          get

          {

             yield return Apple;

             yield return Banana;

             yield return Cherry;

          }

       }

     

    It is also possible to use the Reflection techniques
    • Edited by Viorel_MVP Sunday, May 20, 2018 6:38 PM
    • Marked as answer by E-John Monday, May 21, 2018 5:45 AM
    Sunday, May 20, 2018 6:36 PM

All replies

  • Dear All,

    How to iterate all members of type-safe enum design pattern by foreach()?

    The expected should be 

    Branch.Apple.EN;

    Branch.Banana.EN;

    Branch.Cherry.EN;

        public sealed class Branch
        {
            public static Branch Apple { get { return new Branch(1, "蘋果", "Apple"); } }
            public static Branch Banana { get { return new Branch(2, "香蕉", "Banana"); } }
            public static Branch Cherry { get { return new Branch(3, "櫻桃", "Cherry"); } }
    
            public readonly int Number;
            public readonly string TW;
            public readonly string EN;
    
            private Branch(int Number, string TW, string EN)
            {
                this.Number = Number;
                this.TW = TW;
                this.EN = EN;
            }
        }

    Thanks and best regards,

    E-John

    Please rephrase your question, I see no enum in that sample code...
    Sunday, May 20, 2018 5:36 PM
  • The next loop will work:

       foreach( var b in Branch.Values )

       {

          // . . .

       }

     

    if you add the next member to the Branch class:

       public static IEnumerable<Branch> Values

       {

          get

          {

             yield return Apple;

             yield return Banana;

             yield return Cherry;

          }

       }

     

    It is also possible to use the Reflection techniques
    • Edited by Viorel_MVP Sunday, May 20, 2018 6:38 PM
    • Marked as answer by E-John Monday, May 21, 2018 5:45 AM
    Sunday, May 20, 2018 6:36 PM
  • Hi Captain Kernel,

    The name "type-safe enum design pattern" is referred to the link,

    https://stackoverflow.com/questions/1038321/alternative-to-enum-in-java-1-4

    As far as I know, it comes from Java. But who created this kind of design pattern, I don't know.

    It is very welcome if someone knows who creates this kind of design pattern.

    This kind of definition of class make user to refer the class member like enum item,

    so that's why it called type-safe enum pattern, I think.

    That's 

    Branch.Apple.EN,

    Branch.Banana.EN,...etc,

    It is similar to get the value in enum type 

    BranchEnum.Apple, BranchEnum.Banana,...etc.

        public enum BranchEnum
        {
            Apple = 0x01,
            Banana = 0x02,
            Cherry = 0x03,
        }

    Thanks and regards,

    E-John

    Monday, May 21, 2018 5:41 AM
  • Hi Viorel_

    It works, thanks for your great helps.

    Thanks and Best regards,

    E-John

    namespace TestTypeSafeEnum
    {
        public sealed class Branch
        {
            public static Branch Apple { get { return new Branch(1, "蘋果", "Apple"); } }
            public static Branch Banana { get { return new Branch(2, "香蕉", "Banana"); } }
            public static Branch Cherry { get { return new Branch(3, "櫻桃", "Cherry"); } }
    
            public readonly int Number;
            public readonly string TW;
            public readonly string EN;
            static int c;
    
            private Branch(int Number, string TW, string EN)
            {
                this.Number = Number;
                this.TW = TW;
                this.EN = EN;
            }
    
            public static IEnumerable<Branch> Values
            {
                get
                {
                    yield return Apple;
                    yield return Banana;
                    yield return Cherry;
                }
            }
    
            public static int GetCount()
            {
                c = 0;
                foreach (var b in Branch.Values)
                {
                    c++;
                }
                return c;
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
    
                Console.WriteLine("Item number: {0}", Branch.GetCount());
                Console.WriteLine("Iterate all items in English");
                foreach (var b in Branch.Values)
                {
                    Console.WriteLine("Number: {0}, Name: {1}", b.Number, b.EN);
                }
                Console.WriteLine(new String('-', 60));
    
                Console.WriteLine("Item number: {0}", Branch.GetCount());
                Console.WriteLine("Iterate all items in Traditional Chinese");
                foreach (var b in Branch.Values)
                {
                    Console.WriteLine("Number: {0}, Name: {1}", b.Number, b.TW);
                }
                Console.WriteLine(new String('-', 60));
    
                Console.WriteLine("Item number: {0}", Branch.GetCount());
                Console.WriteLine("Find item in type-safe enum");
                foreach (var b in Branch.Values)
                {
                    string itemBeFound = "蘋果";
                    if (b.TW == itemBeFound)
                    {
                        Console.WriteLine("Found items, Numer: {0}, TW name: {1}", b.Number, b.TW);
                    }
                }
    
                Console.ReadLine();
            }
        }
    }
    

    Monday, May 21, 2018 5:45 AM