anyone offer a better way?

C

cj

I have menuitems that are checked or not checked for each day of the
week. If it's checked action will be taken on that day. There is also
the time of day that the action takes place to consider. This function
(which I haven't even tested yet) should find the next time action will
take place based on what days are checked and the time passed to it.

Actually I do this backward of what I'd like cause well I would break
out of the loop when I found the first time but that functionality isn't
available in VS2003 (though I could use a GOTO to approximate it).

Just curious what you might come up with. the long if statement is my
main objection to this method. But mostly just wondering what other
approaches someone might have.

Private Function NextAction(ByVal ActionTime As String) As String
Dim ActionDateTime As Date

NextAction = "Disabled"

For x As Int32 = 7 To 0 Step -1
ActionDateTime =
Now.Date.AddDays(x).AddTicks(TimeValue(ActionTime).Ticks)
If ActionDateTime.DayOfWeek = DayOfWeek.Sunday And
SunMenuItem.Checked Or _
ActionDateTime.DayOfWeek = DayOfWeek.Monday And
MonMenuItem.Checked Or _
ActionDateTime.DayOfWeek = DayOfWeek.Tuesday And
TueMenuItem.Checked Or _
ActionDateTime.DayOfWeek = DayOfWeek.Wednesday And
WedMenuItem.Checked Or _
ActionDateTime.DayOfWeek = DayOfWeek.Thursday And
ThuMenuItem.Checked Or _
ActionDateTime.DayOfWeek = DayOfWeek.Friday And
FriMenuItem.Checked Or _
ActionDateTime.DayOfWeek = DayOfWeek.Saturday And
SatMenuItem.Checked Then

If ActionDateTime > Now Then 'if x=0 (today) it might
be too late in the day
NextAction = ActionDateTime.ToShortDateString
End If
End If
Next
End Function
 
C

Chris

cj said:
I have menuitems that are checked or not checked for each day of the
week. If it's checked action will be taken on that day. There is also
the time of day that the action takes place to consider. This function
(which I haven't even tested yet) should find the next time action will
take place based on what days are checked and the time passed to it.

Actually I do this backward of what I'd like cause well I would break
out of the loop when I found the first time but that functionality isn't
available in VS2003 (though I could use a GOTO to approximate it).

Just curious what you might come up with. the long if statement is my
main objection to this method. But mostly just wondering what other
approaches someone might have.

Private Function NextAction(ByVal ActionTime As String) As String
Dim ActionDateTime As Date

NextAction = "Disabled"

For x As Int32 = 7 To 0 Step -1
ActionDateTime =
Now.Date.AddDays(x).AddTicks(TimeValue(ActionTime).Ticks)
If ActionDateTime.DayOfWeek = DayOfWeek.Sunday And
SunMenuItem.Checked Or _
ActionDateTime.DayOfWeek = DayOfWeek.Monday And
MonMenuItem.Checked Or _
ActionDateTime.DayOfWeek = DayOfWeek.Tuesday And
TueMenuItem.Checked Or _
ActionDateTime.DayOfWeek = DayOfWeek.Wednesday And
WedMenuItem.Checked Or _
ActionDateTime.DayOfWeek = DayOfWeek.Thursday And
ThuMenuItem.Checked Or _
ActionDateTime.DayOfWeek = DayOfWeek.Friday And
FriMenuItem.Checked Or _
ActionDateTime.DayOfWeek = DayOfWeek.Saturday And
SatMenuItem.Checked Then

If ActionDateTime > Now Then 'if x=0 (today) it might
be too late in the day
NextAction = ActionDateTime.ToShortDateString
End If
End If
Next
End Function

You can break out of a loop:

For x As Integer = 0 To 10
If True Then
Exit For
End If
Next
 
G

Guest

How about something like this:

Dim ActionNeeded as Boolean = False
For x as Int32 = 0 to 6
ActionDateTime = Now.Date.AddDays(x).AddTicks(TimeValue(ActionTime).Ticks)
Select Case ActionDateTime.DayOfWeek
Case DayOfWeek.Sunday
ActionNeeded = SunMenuItem.Checked
Case DayOfWeek.Monday
ActionNeeded = MonMenuItem.Checked
.....Etc
End Select
If ActionNeeded then
If ActionDateTime > Now then
Return ActionDateTime.ToShortDateString
EndIf
ActionNeeded = False
Next For
End Function
 
C

cj

Quite true, it was too late inthe day to be thinking straight. I was
thinking about the other way. Forcing to jump to the next iteration.
 
Top