25.1 Specifying different int values for enum elements


25.1 Specifying different int values for enum elements

Although numbering starts from 0 by default, if you have a good reason for assigning each element in the enum a different int value, you can specify counting to start from any other value:

 enum Month {Jan=1,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec}; 

In this case, Month.Jan will be 1 , Month.Feb will be 2 and Month.Dec will be 12 .

In fact, you can specify numbering for each int element in an enum, as this example shows: [2]

[2] There had better be a good reason for doing this!

 1:  using System;  2:  3:  public class TestClass{  4:    enum Month {  5:      Jan=12,  6:      Feb=5,  7:      Mar=3,  8:      Apr, // 4  9:      May, // 5 10:      Jun=9, 11:      Jul, // 10 12:      Aug, // 11 13:      Sep, // 12 14:      Oct, // 13 15:      Nov=4, 16:      Dec // 5 17:    }; 18: 19:    public static void Main(){ 20:      Console.WriteLine("Jan: "+(int)Month.Jan); 21:      Console.WriteLine("Feb: "+(int)Month.Feb); 22:      Console.WriteLine("Mar: "+(int)Month.Mar); 23:      Console.WriteLine("Apr: "+(int)Month.Apr); 24:      Console.WriteLine("May: "+(int)Month.May); 25:      Console.WriteLine("Jun: "+(int)Month.Jun); 26:      Console.WriteLine("Jul: "+(int)Month.Jul); 27:      Console.WriteLine("Aug: "+(int)Month.Aug); 28:      Console.WriteLine("Sep: "+(int)Month.Sep); 29:      Console.WriteLine("Oct: "+(int)Month.Oct); 30:      Console.WriteLine("Nov: "+(int)Month.Nov); 31:      Console.WriteLine("Dec: "+(int)Month.Dec); 32:    } 33:  } 

Output:

 c:\expt>test Jan: 12 Feb: 5 Mar: 3 Apr: 4 May: 5 Jun: 9 Jul: 10 Aug: 11 Sep: 12 Oct: 13 Nov: 4 Dec: 5 

Examine lines 10 “ 14. Since Month.Jun is assigned the value of 9 , the next int element ( Month.Jul ) will be automatically assigned the next larger int value because an explicit value hasn't been given to it. Note that you can have enum elements sharing identical int values (in this case, both Month.Nov and Month.Apr have 4 as their values).

Your enumeration can be assigned accessibility protection too:

  public  enum Month {Jan=1,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec}; 

makes the Month enum available to any other class.

Enums are useful for comparing return values from methods without really bothering about what its underlying int value is. For example:

 1:  using System;  2:  3:  public class TestClass{  4:  enum Months  5:  {Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec};  6:  7:    public static void Main(){  8:      TestClass tc = new TestClass();  9:      if (tc.DoSomething() ==  Months.Feb  ){ 10:        // other codes 11:      } 12:    } 13: 14:    Months DoSomething (){ 15:      return Months.Feb; 16:    } 17:  } 

This is usually accomplished by using final variables in Java. But enums gives a more elegant and convenient way to do this.



From Java to C#. A Developers Guide
From Java to C#: A Developers Guide
ISBN: 0321136225
EAN: 2147483647
Year: 2003
Pages: 221
Authors: Heng Ngee Mok

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net