一个return的低级错误

来源:互联网 发布:淘宝网拍卖茅台酒 编辑:程序博客网 时间:2024/05/22 03:53

之前在项目中写了这样一个VB.NET的方法,类似如下:

Function VerifyDate() As Boolean
        Dim flag As Boolean = True
        Dim a As Integer = 1
        Dim b As Integer = 3
        Dim c As Integer = 7
        If b < c Then--验证1
            Return flag = True
        Else
            Return flag = False
        End If
        If a > 0 Then--验证2
            Return flag = False
        Else
            Return flag = False
        End If
    End Function

本来自已想达到的效果是:只有flag为False的时候,才会返回。可是程序在验证1为true的情况下,直接返回,没有验证第二个。最后我修改为如下:

Function VerifyDateNew() As Boolean
        Dim flag As Boolean = True
        Dim a As Integer = 1
        Dim b As Integer = 3
        Dim c As Integer = 7
        If b >= c Then
            Return flag = False
        End If
        If a <= 0 Then
            Return flag = False
        End If
    End Function

这样的话,只有验证没有通过的话才会返回False值,这样不用担心会跳过后面的验证了。哎!难怪客户说“你们开发人员怎么会犯这么低级的错误”。我感觉好惭愧啊!我以后要好好对自已的程序负责了,对客户负责了。

原创粉丝点击