简体   繁体   English

我在哪里缺少此VB.NET函数中的“返回”语句?

[英]Where am I missing a 'Return' statement in this VB.NET function?

I am getting the error 我收到错误

Function 'Login' doesn't return a value on all code paths. 函数“登录”并非在所有代码路径上都返回值。 Are you missing a 'Return' statement? 您是否缺少“退货”声明?

which I understand to a certain extent from viewing various questions on Stack Overflow. 通过查看有关Stack Overflow的各种问题,我在一定程度上了解了这一点。 My confusion is that I have a return value for all code paths - don't I? 我的困惑是我对所有代码路径都有返回值-是吗?

Public Function Login(Username As String, Password As Integer) As Integer

    Dim tableAdapter As New AcquisitionPortalDataSetTableAdapters.ITVF_LoginLogoutTableAdapter
    Dim dt As DataTable = tableAdapter.GetData(Username, Convert.ToInt32(Password))

    Try
        If dt IsNot Nothing AndAlso dt.Rows.Count > 0 Then
            For Each dr In dt.Rows
                If dr("LAN").ToString = Username AndAlso Convert.ToInt32(dr("Code")) = Password Then
                    GlobalVariables.iUserType = Convert.ToInt32(dr("Code"))
                    GlobalVariables.iUserID = Convert.ToInt32(dr("ID"))
                    Return 1
                Else
                    Return 0
                End If
            Next
        Else
            Return 0
        End If
    Catch
        Return 0
    End Try

End Function

I've gone over this many times and each statement can be evaluated as far as I can tell. 我已经看了很多遍了,据我所知,每个语句都可以进行评估。 Can anyone advise otherwise, ideally with a brief explanation if possible, so I understand for the future? 任何人都可以提出其他建议,最好在可能的情况下进行简要说明,以便我对未来有所了解?

It is because the compiler doesn't know that you are checking if the table has rows and so assumes that the For Each loop may not be entered into by your code path. 这是因为编译器不知道您正在检查表是否包含行,因此假定您的代码路径可能未输入For Each循环。 In fact there isn't really any need to check the number of rows first because the For Each loop will have zero iterations if there are no rows in it anyway. 实际上,实际上并不需要先检查行数,因为如果其中没有行,则“ For Each循环的迭代次数为零。

To fix the warning you need to cover that eventuality explicitly: 要解决该警告,您需要明确地涵盖这种可能性:

Public Function Login(Username As String, Password As Integer) As Integer

    Dim tableAdapter As New AcquisitionPortalDataSetTableAdapters.ITVF_LoginLogoutTableAdapter
    Dim dt As DataTable = tableAdapter.GetData(Username, Convert.ToInt32(Password))

    Try
        If dt IsNot Nothing AndAlso dt.Rows.Count > 0 Then
            For Each dr In dt.Rows
                If dr("LAN").ToString = Username AndAlso Convert.ToInt32(dr("Code")) = Password Then
                    GlobalVariables.iUserType = Convert.ToInt32(dr("Code"))
                    GlobalVariables.iUserID = Convert.ToInt32(dr("ID"))
                    Return 1
                Else
                    Return 0
                End If
            Next
            Return 0 'return here when there are no rows in the datatable
        Else
            Return 0
        End If
    Catch
        Return 0
    End Try

End Function

Having said that you function can be made simpler (and quicker) if your usernames are unique because if you find the usernme you don't have to check for any more: 话虽如此,如果您的用户名是唯一的,则可以使您的功能变得更加简单(快捷),因为如果您找到了usernme,就不必再检查了:

Public Function Login(Username As String, Password As Integer) As Integer

    Dim tableAdapter As New AcquisitionPortalDataSetTableAdapters.ITVF_LoginLogoutTableAdapter
    Dim dt As DataTable = tableAdapter.GetData(Username, Convert.ToInt32(Password))
    If dt Is Nothing Then Return 0

    For Each dr As DataRow In dt.Rows
        If dr.Field(Of String)("LAN") = Username Then
            'we found the username now check the password
            Return If(dr.Field(Of Int32)("Code") = Password, 1, 0)
        End If
    Next

    'we didn't find a matching username
    Return 0

End Function

Can't you just put one return statement after the End Try ? 您能否在End Try之后仅放一个return语句? And make a variable at the start of the function which holds the result? 并在保存结果的函数开头创建一个变量? At the end of the function, just return that result. 在函数的末尾,只需返回该结果即可。

I'm not familiar with VB.NET, but that should do the trick. 我不熟悉VB.NET,但这可以解决问题。

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

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