如何检查星期几是周末和结束计划? [英] How do I check if day of week is weekend and end program?
问题描述
我想查看星期几是否是周末,如果是,发布警告信息并结束程序。
我的elseif语句不起作用( ElseIf intDayOfWeek = 1 OrElse intDayOfWeek = 7然后)
共享 < span class =code-keyword> Sub main()
Dim objJobExecution As 新 Msc.Integration.MessageBroker.Library.v4.JobExecution( ServiceCatalog, 文档发布, 查找)
Dim blnWarningFlag As Boolean = False
Dim dtmTime = 新 TimeSpan(DateTime.Now.Hour,DateTime.Now.Minute, 0 )
Dim dblMinutes As Double = dtmTime.TotalMinutes
Dim objLookupList As List(中跨度> Msc.Integration.Mncis.Library.v4.DocumentPublishingLookup)= <跨度类= 代码关键字>没有跨度>
<跨度类=代码-keyword> Dim intDayOfWeek 作为 整数 = DateTime.Today.DayOfWeek
尝试
' 比较当前计划分钟的分钟
如果(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 )然后
' 获取文件的调用方法
objJobExecution.AddExecutionStep( 调用GetAll跨度>)
objLookupList = Msc.Integration.Mncis.Library.v4.DocumentPublishingLookup.GetAllReadyForPublishing()
<跨度类= 代码关键字> elseif的跨度>(dblMinutes> = <跨度class =code-digit> 540 AndAlso dblMinutes< = 1020 )然后
' 获取文档的调用方法
objJobExecution.AddExecutionStep(<跨度类= 代码串> 跨度> <跨度类= 代码串 >调用GetLast60跨度>)
objLookupList = MSC。 Integration.Mncis.Library.v4.DocumentPublishingLookup.GetLast60MinReadyForPublishing()
' 检查星期几是否为周末并发布警告消息并退出程序
ElseIf intDayOfWeek = 1 OrElse intDayOfWeek = 7 然后
objJobExecution.AddExecutionStep( 日程安排之外什么也不做)
blnWarningFlag = True
结束 如果
结束 Sub
我尝试过:
<跨度类= 代码关键字> elseif的跨度> intDayOfWeek = <跨度类= 代码位> 1 跨度> <跨度类= 代码关键字> OrElse运算跨度> intDayOfWeek = 7 然后
objJobExecution.AddExecutionStep( 计划外无所事事)
blnWarningFlag = 真
结束 如果
< blockquote>查看 DayOfWeek枚举(系统) [ ^ ]:
DayOfWeek枚举表示每周七天的日历中的星期几。此枚举中常量的值范围从DayOfWeek.Sunday到DayOfWeek.Saturday。如果强制转换为整数,则其值的范围从零(表示DayOfWeek.Sunday)到六(表示DayOfWeek.Saturday)。
如您所见,1。一天不是星期天,七天不是星期六!
所以你的代码应该是:
如果 intDayOfWeek = 0 或 intDayOfWeek = 6 然后
更好地使用这样的常量:
如果跨度> intDayOfWeek = DayOfWeek.Saturday <跨度类= 代码关键字>或者跨度> intDayOfWeek = DayOfWeek.Sunday <跨度类= 代码关键字>然后跨度>
objJobExecution.AddExecutionStep( 计划外无所事事)
blnWarningFlag = 真
结束 如果
从调试器:在函数的第一行放置一个断点,并通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。
猜测,它正在通过第一次测试,或者可能是第二次测试,所以它永远不会到第三次 - 你学习一种新的(非常非常有用的)技能:调试!
你应该试试
ElseIf intDayOfWeek = 1 或 intDayOfWeek = 7 然后
oops,我的错误。
首先,删除尝试
并查看是否有错误消息。
使用尝试
而不使用catch
只有在代码中出现问题时才会被告知。建议:只有在你有一个非常愚蠢的理由时才使用try / catch,并且只在调试你的代码之后才使用。
当你不明白你的代码时正在做或为什么它做它做的,答案是调试器。
使用调试器来查看你的代码在做什么。它允许你逐行执行第1行并在执行时检查变量,它是一个令人难以置信的学习工具。
调试器 - 维基百科,免费的百科全书 [ ^ ]
掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]
调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做的比较。
调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。
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)
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:
ElseIf intDayOfWeek = 1 OrElse intDayOfWeek = 7 Then
objJobExecution.AddExecutionStep("Outside of schedule do nothing")
blnWarningFlag = True
End If
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:
If intDayOfWeek = 0 Or intDayOfWeek = 6 Then
Better you use the constants like this:
If intDayOfWeek = DayOfWeek.Saturday Or intDayOfWeek = DayOfWeek.Sunday Then objJobExecution.AddExecutionStep("Outside of schedule do nothing") blnWarningFlag = True End If
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!
You should try
ElseIf intDayOfWeek = 1 Or intDayOfWeek = 7 Then
oops, my mistake.
First thing, remove thetry
and see if there is an error message.
Using atry
without acatch
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.
这篇关于如何检查星期几是周末和结束计划?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!