我写了一个私有子,它获取当前时间并计算当前时间落入哪个时间段(从时间跨度数组)。鉴于我正在测试十个时间括号,代码是重复的,只有数组的整数索引和每个if语句之间的结果整数变化。
If DateTime.Now.TimeOfDay >= tmeToday(0) AndAlso DateTime.Now.TimeOfDay <= tmeToday(1) Then
intPeriodFrid = 0 'ext morning; 8am - 9am
intNextBell = 1
ElseIf DateTime.Now.TimeOfDay >= tmeToday(1) AndAlso DateTime.Now.TimeOfDay <= tmeToday(2) Then
intPeriodFrid = 1 'morning break; 9am - 9:05am
intNextBell = 2
......以后可笑的重复次数
ElseIf DateTime.Now.TimeOfDay >= tmeToday(10) AndAlso DateTime.Now.TimeOfDay <= tmeToday(11) Then
intPeriodFrid = 10 'ext afternoon; 3:10pm - 4:10pm
intNextBell = 11
End If
然后它击中了我可能会这样做:
For value As Integer = 0 To 10
If DateTime.Now.TimeOfDay >= tmeToday(value) AndAlso DateTime.Now.TimeOfDay <= tmeToday(value + 1) Then
intPeriodFrid = value
intNextBell = (value + 1)
Exit For
End If
Next
这将是我第一次使用循环(我已经进入基于VB.NET的课程十周了),所以我对此作为解决方案缺乏信心,它会起作用吗?如果我实现这个,我需要注意哪些弱点/问题?