简体   繁体   English

这个vb.net代码有什么问题?

[英]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 ! 它将始终在label5中显示味精! Ticket cannot be cancelled on same day or after journey date if i only use a single end if statement then it works fine. 如果我只使用单端if语句,则无法在当天或旅程日期之后取消机票,那么它可以正常工作。 ... and if i use 3 conditions as mentioned above it will display error message in label5 as Sorry ! ...并且如果我使用上述3个条件,它将在label5中显示错误消息,对不起! 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 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

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. 您正在尝试将字符串分配给Date变量。 Use DateTime.TryParse or DateTime.TryParseExact to safely convert the string to a date. 使用DateTime.TryParseDateTime.TryParseExact将字符串安全地转换为日期。

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. 您正在创建要转换为字符串的DateTime值,然后将其转换回DateTime值以进行比较。 Why not use the DateTime values in the first place? 为什么不首先使用DateTime值? 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? 为什么使用Label.Text属性进行比较?

The labels do not have meaningful names and they are cast from DateTime variables you already have. 标签没有有意义的名称,它们是从您已有的DateTime变量中强制转换的。

You have three conditions chained by ElseIf s that all result in the same action, this should be one If with OrElse s. 你必须通过链接三个条件ElseIf s表示所有产生相同的作用,这应该是一个IfOrElse秒。

As Fredrik points out, that intial implicit assign would fail a strict conversion. 正如Fredrik指出的那样,该初始隐式分配将无法进行严格的转换。

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. 我仍然不知道您实际想要实现什么,但是我希望重写可以帮助您了解实际操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM