目的:排出 2010年1~12月的A.P.N排班記錄 

void Main()

{
IList<Schedule> schedules = new List<Schedule>();
int year = 2010;
int[] months = {1,2,3,4,5,6,7,8,9,10,11,12};
foreach(int i in months){
int maxDays = DateTime.DaysInMonth(year, i);
for(int j=1; j<=maxDays; j++){
foreach(string n in Enum.GetNames(typeof(ScheduleMode))){
schedules.Add(new Schedule(){
Year = year,
Month = i,
Date = j,
Mode = (ScheduleMode)Enum.Parse(typeof(ScheduleMode), n),
Description = string.Format("Work on {3}, {0}/{1}/{2}", year,i,j, new DateTime(year,i,j).DayOfWeek)
});
}
}
}
schedules.Dump();
}

// Define other methods and classes here
public enum ScheduleMode {
A,P,N
}
public class Schedule{
public int Year {get;set;}
public int Month {get;set;}
public int Date {get;set;}
public ScheduleMode Mode {get;set;}
public string Description {get;set;}
}

//////////////////////////////////////////////////////////////////////////////////////

 這裏隱含了另一個功能,可以通過Enum.GetNames(typeof(ScheduleMode))獲得枚舉類型的各項名稱(用於綁定到UI界面),還可以通過(ScheduleMode)Enum.Parse(typeof(ScheduleMode), n)將某個字符串轉換成枚舉類型(用於從數據庫查詢到的值轉換成枚舉類型),這樣我們就可以很方便地將數據庫中的查詢到的值轉換成“預先”定義好的枚舉類型(可以有“名稱”和“值”)。

相关文章:

  • 2021-05-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-09
  • 2022-12-23
  • 2022-02-03
猜你喜欢
  • 2022-01-19
  • 2021-07-07
  • 2021-08-06
  • 2021-12-31
  • 2022-12-23
  • 2021-09-10
  • 2021-09-19
相关资源
相似解决方案