gpt4 book ai didi

.net - 我在哪里缺少此VB.NET函数中的 'Return'语句?

转载 作者:行者123 更新时间:2023-12-03 08:04:33 25 4
gpt4 key购买 nike

我收到错误

Function 'Login' doesn't return a value on all code paths. Are you missing a 'Return' statement?



通过查看Stack Overflow上的各种问题,我在一定程度上了解了这一点。我的困惑是我对所有代码路径都有返回值,不是吗?
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

我已经看了很多遍了,据我所知,每个语句都可以进行评估。任何人都可以以其他方式提出建议,如果可能的话,最好在简短的解释下,以便我对 future 有所了解?

最佳答案

这是因为编译器不知道您正在检查表是否包含行,因此假定您的代码路径可能未输入For Each循环。实际上,实际上并不需要先检查行数,因为如果反行中没有行,则For Each循环的迭代次数为零。

要解决该警告,您需要明确地涵盖这种可能性:

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

话虽如此,如果您的用户名是唯一的,则可以使您的功能变得更加简单(快捷),因为如果您找到了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

关于.net - 我在哪里缺少此VB.NET函数中的 'Return'语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27564743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com