开发者

whats wrong in this vb.net code?

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

error :

it will always displays the msg in label5 Sorry ! Ticket cannot be cancelled on same day or after journey date if i only use a single end if statement then it works fine. ... and if i use 3 conditions as mentioned above it will display error message in label5 as Sorry ! Ticket cannot be cancelled on same day or after journey date

if i use this instead of above code ....then it works fine

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 afte开发者_C百科r 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


The most obvious thing that I can see without running the code is this:

Dim UTCTime As Date = TextBox1.Text

You are trying to assign a string to a Date variable. Use DateTime.TryParse or DateTime.TryParseExact to safely convert the string to a date.

Update
I have now looked at your code more closely, and found the following:

  • You are creating DateTime values that you convert to strings that you convert back to DateTime values for comparison. Why not use the DateTime values in the first place? Each conversion is a possible point of failure.
  • You are making comparisons on three different time intervals, but each leads to the same error message. This means that if the comparison with the smallest date fails, there is no need to test for the others, since it will lead to the same result.

So I suggest you write your method like this instead:

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


Why use the Label.Text properties to do your comparisons?

The labels do not have meaningful names and they are cast from DateTime variables you already have.

You have three conditions chained by ElseIfs that all result in the same action, this should be one If with OrElses.

As Fredrik points out, that intial implicit assign would fail a strict conversion.

To simplify:

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 

I reworked your function a little. I still have no idea what you actually want to achieve but I hope the rewrite helps you see what you are actually doing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜