Click here to Skip to main content
15,889,200 members
Please Sign up or sign in to vote.
1.80/5 (3 votes)
See more:
I would like to check if day of week is weekend and if it is, post a warning message and end program.
My elseif statement isn't working (ElseIf intDayOfWeek = 1 OrElse intDayOfWeek = 7 Then)

VB
Shared Sub main()
        Dim objJobExecution As New Msc.Integration.MessageBroker.Library.v4.JobExecution("ServiceCatalog", "Document Publishing", "Lookup")
        Dim blnWarningFlag As Boolean = False
        Dim dtmTime = New TimeSpan(DateTime.Now.Hour, DateTime.Now.Minute, 0)
        Dim dblMinutes As Double = dtmTime.TotalMinutes
        Dim objLookupList As List(Of Msc.Integration.Mncis.Library.v4.DocumentPublishingLookup) = Nothing
        Dim intDayOfWeek As Integer = DateTime.Today.DayOfWeek

        Try
            'Compare current minutes with schedule minutes
            If (dblMinutes >= 480 AndAlso dblMinutes < 540) OrElse
                (dblMinutes >= 660 AndAlso dblMinutes < 665) OrElse
                (dblMinutes >= 780 AndAlso dblMinutes < 785) OrElse
                (dblMinutes >= 900 AndAlso dblMinutes < 905) OrElse
                (dblMinutes >= 1020 AndAlso dblMinutes <= 1025) Then
                'Call method to get documents
                objJobExecution.AddExecutionStep("Calling GetAll")
                objLookupList = Msc.Integration.Mncis.Library.v4.DocumentPublishingLookup.GetAllReadyForPublishing()
            ElseIf (dblMinutes >= 540 AndAlso dblMinutes <= 1020) Then
                'Call method to get documents
                objJobExecution.AddExecutionStep("Calling GetLast60")
                objLookupList = Msc.Integration.Mncis.Library.v4.DocumentPublishingLookup.GetLast60MinReadyForPublishing()
                'Check if day of week is weekend and post warning message and exit program
            ElseIf intDayOfWeek = 1 OrElse intDayOfWeek = 7 Then
                objJobExecution.AddExecutionStep("Outside of schedule do nothing")
                blnWarningFlag = True
            End If
    End Sub


What I have tried:

VB
ElseIf intDayOfWeek = 1 OrElse intDayOfWeek = 7 Then

                objJobExecution.AddExecutionStep("Outside of schedule do nothing")
                blnWarningFlag = True
End If
Posted
Updated 14-Feb-17 6:56am
Comments
[no name] 14-Feb-17 11:37am    
Use your debugger to find out what "not working" means. DayOfWeek is an enum and is numbered from 0 to 6 so it would never be 7.
Richard Deeming 14-Feb-17 12:51pm    
You do it in exactly the same way you were told when you posted the same question three days ago!
How can I figure out if the day is saturday or sunday?[^]

Take a look at DayOfWeek Enumeration (System)[^] :

The DayOfWeek enumeration represents the day of the week in calendars that have seven days per week. The value of the constants in this enumeration ranges from DayOfWeek.Sunday to DayOfWeek.Saturday. If cast to an integer, its value ranges from zero (which indicates DayOfWeek.Sunday) to six (which indicates DayOfWeek.Saturday).

As you see, the 1. day isn't Sunday and the 7.day isn't saturday!
So your code should be:

VB
If intDayOfWeek = 0 Or intDayOfWeek = 6 Then


Better you use the constants like this:

VB
If intDayOfWeek = DayOfWeek.Saturday Or intDayOfWeek = DayOfWeek.Sunday Then
 objJobExecution.AddExecutionStep("Outside of schedule do nothing")
 blnWarningFlag = True
End If
 
Share this answer
 
v7
Comments
Member 11403304 14-Feb-17 11:47am    
Thanks _duDE, I added the code you suggested. In debug how to I trick the program to think today is Sunday or Saturday so it can run the 3rd option.
Start with the debugger: Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

At a guess, it's passing the first test or possibly the second so it never gets to the third - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Comments
Member 11403304 14-Feb-17 12:15pm    
I added the code as suggested by _duDE but I am not sure how in debug mode, I can trick the program to think today is Sunday (6) or Saturday (0) so it can run the 3rd option.
You should try
VB
ElseIf intDayOfWeek = 1 Or intDayOfWeek = 7 Then

oops, my mistake.
First thing, remove the try and see if there is an error message.
Using a try without a catch only prevent yourself from being told if there is a problem in your code. Advice: use try/catch only if you have a very goof reason to do it, and only after debugging your code.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900