そのような場合には列挙型を使用することをお勧めします。列挙型でできることはたくさんあります。例を挙げます。
列挙定義:
public enum Month
{ Jan=1, Feb, Mar, Apr, may, Jun, Jul, Aug, Sep, Oct, Nov, Dec }
ボタンクリックで:
Month current = Month.Jan; //staticly checking a month
if(current == Month.Jan)
MessageBox.Show("It's Jan");
else
MessageBox.Show("It's not Jan.");
List<Month> specialMonthes = new List<Month>();
specialMonthes.Add(Month.Oct);
specialMonthes.Add(Month.Apr);
specialMonthes.Add(Month.Jan);
//Search the list for the month we are in now
foreach (Month specialMonth in specialMonthes)
{
if ((int)specialMonth == DateTime.Now.Month) //dynamically checking this month
MessageBox.Show(string.Format("It's {0} now & {0} is a special month.",
specialMonth));
//Output: It's Jan now and Jan is a special month.
}
呪文を唱えることも、比較することも、キャストすることもできます。
では、列挙型を使用しないのはなぜですか? 車があれば走る必要はありません。