我可以使用vb.net中的select case语句检查时隙 [英] Can i check time slot using select case statement in vb.net
本文介绍了我可以使用vb.net中的select case语句检查时隙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用半小时的时段检查当前时间(从00:00,00:30,01:00,...... 23:30共48次开始)。是否可以使用select case语句。如果没有任何其他建议,请?
我将当前时间存储在一个对象中。现在我需要对这个变量进行条件检查。
Dim datime As DateTime = DateTime.Now.ToString(t)
I want to check the current time with the half an hour time slots (total of 48 starts from 00:00,00:30,01:00,....23:30). Is it possible using select case statement. If not any other suggestions please?
I am storing the current time in an object. now i need to do conditional check on this variable.
Dim datime As DateTime = DateTime.Now.ToString("t")
推荐答案
您需要实现计时器 [< a href =http://msdn.microsoft.com/en-us/library/system.threading.timer.aspxtarget =_ blanktitle =New Window> ^ ],它提供了按指定的时间间隔执行方法的机制。
--EDIT1 ---
好的,创建新的Windows应用程序项目并放置标签表格。然后复制并粘贴以下代码:
You need to implement timer[^], which provides a mechanism for executing a method at specified intervals.
--EDIT1---
OK, create new windows application project and place label on the form. Then copy and paste code below:
Public Class Form1
Delegate Sub SetTextCallback(ByVal [text] As String)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Create an inferred delegate that invokes methods for the timer.
Dim tcb As TimerCallback = AddressOf CheckSendReport
Dim oTimer As System.Threading.Timer = New System.Threading.Timer(tcb, 0, 0, 60000)
End Sub
'This method is called by the timer delegate.
'Every (period time - in example 60000) we call DrawMessage sub
Sub CheckSendReport(ByVal state As Object)
Dim dTime As Date = Date.Now
If dTime.Minute = 30 Then
DrawMessage(dTime.ToString & " - Now!")
Else
DrawMessage(dTime.ToString & " - Not yet!")
End If
End Sub
'draw message
Private Sub DrawMessage(ByVal sMsg As String)
If Me.Label1.InvokeRequired Then
Dim d As New SetTextCallback(AddressOf DrawMessage)
Me.Invoke(d, New Object() {sMsg})
Else
Me.Label1.Text = sMsg
End If
End Sub
End Class
我希望它有用。
I hope it's helpful.
问题是,你不想要,也不能指望一个确切的时间。你最好用
if(datime.TotalMinutes< = 30)
{
}
else if(datime.TotalMinutes< = 60)
{
}
等,而不是switch语句,这将要求您输入每个可能的值。注意我使用'TotalMinutes'如果你使用Minute,它只会达到59.
The problem is, you don't want, and can't count on, an exact time. You're better off with
if (datime.TotalMinutes <= 30)
{
}
else if (datime.TotalMinutes <= 60)
{
}
etc, instead of a switch statement, which would require you entering every possible value. Note I used 'TotalMinutes' if you used Minute, it would only go up to 59.
这篇关于我可以使用vb.net中的select case语句检查时隙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文