好的,我被要求做这个简单的程序,根据星期几的变量输出到文本框。现在这个例子是一个时间表。 我已经完成了变量类,所以我习惯了它。
class Variables
{
public static string Monday = string.Format("Monday's Timetable{0}P1 English{0}P2 Maths{0}P3 History{0}P4 Computing", Environment.NewLine);
public static string Tuesday = string.Format("Tuesday's Timetable{0}P1 Science{0}P2 Geography{0}P3 History{0}P4 Maths", Environment.NewLine);
public static string Wednesday = string.Format("Wednesday's Timetable{0}P1 Science{0}P2 English{0}P3 Computing{0}P4 I.T", Environment.NewLine);
public static string Thursday = string.Format("Thurday's Timetable{0}P1 Geography{0}P2 Geography{0}P3 History{0}P4 Maths", Environment.NewLine);
public static string Friday = string.Format("Friday's Timetable{0}P1 Science{0}P2 Geography{0}P3 History{0}P4 Maths", Environment.NewLine);
}
所有存储的内容,我已经在表格的代码中找到了这个
private void tblcustom_Click(object sender, EventArgs e)
{
string customdayofweek = customdatepicker.Value.DayOfWeek.ToString();
}
private void tbltoday_Click(object sender, EventArgs e)
{
string dayofweektoday = DateTime.Today.DayOfWeek.ToString();
outputtbl.Text = Variables. "Whatever the day of the week is"
}
问题是这样我不需要加载if语句,有没有办法可以做到这一点,所以无论选择星期几还是今天,它都会显示变量。< / p>
我希望很清楚,我似乎无法在互联网上找到它,我也不知道我在寻找什么,这就是为什么我在这里
提前致谢
基兰
答案 0 :(得分:4)
在不更改过多代码的情况下执行此操作的最佳方法是将静态字符串更改为Dictionary<string, string>
。
然后,你可以这样做:
outputtbl.Text = Variables.YourDictionary[dayofweektoday];
答案 1 :(得分:2)
而不是让多个变量使用如下的数组:
string[] timetable = {
string.Format("Monday's Timetable{0}P1 English{0}P2 Maths{0}P3 History{0}P4 Computing", Environment.NewLine),
string.Format("Tuesday's Timetable{0}P1 Science{0}P2 Geography{0}P3 History{0}P4 Maths", Environment.NewLine),
...};
然后您可以使用timetable[(int)DateTime.Today.DayOfWeek]
来选择时间表。
答案 2 :(得分:1)
使用字典存储DayOfWeek和字符串之间的关系。
class Variables
{
Dictionary<DayOfWeek, string> DayText = new Dictionary<DayOfWeek, string>()
{
{DayOfWeek.Monday, string.Format("Monday's Timetable{0}P1 English{0}P2 Maths{0}P3 History{0}P4 Computing", Environment.NewLine)},
{DayOfWeek.Tuesday, string.Format("Tuesday's Timetable{0}P1 Science{0}P2 Geography{0}P3 History{0}P4 Maths", Environment.NewLine)},
{DayOfWeek.Wednesday, string.Format("Wednesday's Timetable{0}P1 Science{0}P2 Geography{0}P3 History{0}P4 Maths", Environment.NewLine)},
{DayOfWeek.Thursday, string.Format("Thursday's Timetable{0}P1 Science{0}P2 Geography{0}P3 History{0}P4 Maths", Environment.NewLine)},
{DayOfWeek.Friday , string.Format("Friday 's Timetable{0}P1 Science{0}P2 Geography{0}P3 History{0}P4 Maths", Environment.NewLine)},
};
}
然后使用当天的枚举值作为键从字典中检索值
private void tbltoday_Click(object sender, EventArgs e)
{
DayOfWeek v = DateTime.Today.DayOfWeek;
if(v != DayOfWeek.Sunday && v != DayOfWeek.Saturday)
outputtbl.Text = Variables.DayText[v];
}
答案 3 :(得分:0)
从c-sharp-get-values-of-static-properties-from-static-class借用概念我在C#Winforms中编写了一个示例代码块。此示例将从Variables中提取当前Day of Week的静态字符串属性。您需要的唯一使用语句是using System;
private void RetrieveDayOfWeekText()
{
Variables myInstance = new Variables();
this.richTextBox1.Text = myInstance.GetType().GetField(DateTime.Now.ToString("dddd")).GetValue(myInstance).ToString();
}