2011-04-20 51 views
0
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click 
     Dim UTCTime As Date = TextBox1.Text 
     Dim IndianTime As DateTime = UTCTime.AddHours(5.5) 
     Dim beforeVal As New TimeSpan(168, 59, 59) 
     Dim beforeVal1 As New TimeSpan(72, 59, 59) 
     Dim beforeVal2 As New TimeSpan(23, 59, 59) 
     Label1.Text = IndianTime.AddSeconds(-beforeVal.TotalSeconds).ToString("G") 
     Label2.Text = IndianTime.AddSeconds(-beforeVal1.TotalSeconds).ToString("G") 
     Label3.Text = UTCTime.AddSeconds(-beforeVal2.TotalSeconds).ToString("G") 

     '//////////// 

     Dim UTCTime1 As Date = Date.UtcNow 
     Dim IndianTime1 As DateTime = UTCTime1.AddHours(5.5) 
     Label4.Text = IndianTime1.ToString("G") 
     If CType(Label4.Text, Date) >= CType(Label3.Text, Date) Then 
      Label5.Text = "Sorry ! Ticket cannot be cancelled on same day or after journey date" 
     ElseIf CType(Label4.Text, Date) >= CType(Label2.Text, Date) Then 
      Label5.Text = "Sorry ! Ticket cannot be cancelled on same day or after journey date" 
     ElseIf CType(Label4.Text, Date) >= CType(Label1.Text, Date) Then 
      Label5.Text = "Sorry ! Ticket cannot be cancelled on same day or after journey date" 
     Else 
      Label5.Text = "Print" 
     End If 



    End Sub 

錯誤:這個vb.net代碼有什麼不對?

它將始終顯示味精label5 對不起!機票不能在同一天或旅程日期後取消 如果我只使用單一結束語句,那麼它工作正常。 ...如果我使用上面提到的3個條件,它會在label5中顯示錯誤信息,如對不起!車票不能在同一天或行程日期後取消

,如果我用這個來代替上面的代碼....然後正常工作

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click 
     Dim UTCTime1 As Date = Date.UtcNow 
     Dim IndianTime1 As DateTime = UTCTime1.AddHours(5.5) 
     Label4.Text = IndianTime1.ToString("G") 
     If CType(Label4.Text, Date) >= CType(Label3.Text, Date) Then 
      Label5.Text = "Sorry ! Ticket cannot be cancelled on same day or after journey date" 
     Else 
      Label5.Text = "Print" 
     End If 
    End Sub 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
     Dim UTCTime As Date = TextBox1.Text 
     Dim IndianTime As DateTime = UTCTime.AddHours(5.5) 
     Dim beforeVal As New TimeSpan(168, 59, 59) 
     Dim beforeVal1 As New TimeSpan(72, 59, 59) 
     Dim beforeVal2 As New TimeSpan(23, 59, 59) 
     Label1.Text = IndianTime.AddSeconds(-beforeVal.TotalSeconds).ToString("G") 
     Label2.Text = IndianTime.AddSeconds(-beforeVal1.TotalSeconds).ToString("G") 
     Label3.Text = UTCTime.AddSeconds(-beforeVal2.TotalSeconds).ToString("G") 
    End Sub 
+0

你能描述你希望它做什麼,和它做什麼呢?你有什麼例外嗎?哪裏? – 2011-04-20 08:03:10

+0

如果我使用單一的,如果然後它的作品,如果我使用嵌套如果然後它不會工作 – sanjeel 2011-04-20 08:21:11

回答

1

,我可以不運行看,最明顯的事情代碼是這樣的:

Dim UTCTime As Date = TextBox1.Text 

您試圖爲Date變量指定一個字符串。使用DateTime.TryParseDateTime.TryParseExact將字符串安全地轉換爲日期。

更新
我現在看着你的代碼更加緊密,並發現:

  • 你創建你轉換爲你轉換回DateTime值比較字符串DateTime值。爲什麼不首先使用DateTime值?每次轉換都是可能的失敗點。
  • 您在三個不同的時間間隔,進行比較,但每次會導致同樣的錯誤消息。這意味着如果與最小日期的比較失敗,則不需要測試其他日期,因爲它會導致相同的結果。

因此,我建議你寫你的方法是這樣,而不是:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click 
    Dim UTCTime As Date 
    If DateTime.TryParse(TextBox1.Text, UTCTime) Then 
     Dim IndianTime As DateTime = UTCTime.AddHours(5.5) 
     Dim comparisonDateTime As DateTime = IndianTime.Add(New TimeSpan(23, 59, 59)) 

     ''#//////////// 

     Dim utcNow As Date = Date.UtcNow 
     Dim IndianTime1 As DateTime = utcNow.AddHours(5.5) 
     If IndianTime1 >= comparisonDateTime Then 
      Label5.Text = "Sorry ! Ticket cannot be cancelled on same day or after journey date" 
     Else 
      Label5.Text = "Print" 
     End If 
    Else 
     ''# TextBox1 did not contain a valid date, inform the user in some way 
    End If 
End Sub 
+0

如何解析...如何使用文本框 – sanjeel 2011-04-20 08:09:26

+0

@sanjeel:請參閱我的答案 – 2011-04-20 08:16:23

+0

中的示例代碼,但它會使條件成立。ii使用單個if else elseif – sanjeel 2011-04-20 08:18:26

0

爲什麼要用Label.Text屬性做你的比較?

標籤沒有有意義的名字,他們是從你已經有日期時間變量投。

您有三個條件鏈接ElseIf s所有導致相同的行爲,這應該是一個IfOrElse s。

正如弗雷德裏克指出,這INTIAL隱含的分配將失敗了嚴格的轉換。

爲了簡化:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click 
    Dim UTCTime As Date = DateTime.Parse(TextBox1.Text) 
    Dim IndianTime As DateTime = UTCTime.AddHours(5.5) 
    Dim IndianTime1 As DateTime = Date.UtcNow.AddHours(5.5) 

    Dim beforeValOffset As New TimeSpan(168, 59, 59) 
    Dim beforeVal1Offset As New TimeSpan(72, 59, 59) 
    Dim beforeVal2OffSet As New TimeSpan(23, 59, 59) 

    Dim beforeVal As DateTime = IndianTime.Subtract(beforeValOffset) 
    Dim beforeVal1 As DateTime = IndianTime.Subtract(beforeVal1Offset) 
    Dim beforeVal2 As DateTime = UTCTime.Subtract(beforeVal2OffSet) 

    Label1.Text = beforeVal.ToString("G") 
    Label2.Text = beforeVal1.ToString("G") 
    Label3.Text = beforeVal2.ToString("G") 
    Label4.Text = IndianTime1.ToString("G") 

    If (IndianTime1 >= beforeVal2) OrElse _ 
      (IndianTime1 >= beforeVal1) OrElse _ 
      (IndianTime1 >= beforeVal) Then 
     Label5.Text = "Sorry ! Ticket cannot be cancelled on same day or after journey date" 
    Else 
     Label5.Text = "Print" 
    Endif 
End Sub 

我返工你的函數一點。我仍然不知道你真的想達到什麼目的,但我希望重寫能幫助你看到你實際上在做什麼。