2011-04-20 104 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 

我返工你的函数一点。我仍然不知道你真的想达到什么目的,但我希望重写能帮助你看到你实际上在做什么。