函数不会在所有代码路径上返回值.使用结果时,运行时可能会出现空引用异常 [英] Function doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used
本文介绍了函数不会在所有代码路径上返回值.使用结果时,运行时可能会出现空引用异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我收到此错误:
函数'getkey'不会在所有代码路径上返回值.结果为时,在运行时可能会发生空引用异常用过的.
Function 'getkey' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.
更改为以下代码:
Public Function getkey(ByVal id As String)
Dim cmd As SqlCommand
Try
cmd = New SqlCommand("dbo.getkeydesc", GenLog.cn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@id", id)
Dim r As SqlDataReader = cmd.ExecuteReader()
If r.HasRows Then
Return True
Else
Return False
End If
Catch ex As Exception
MsgBox(ex.ToString)
Finally
' If Not cn Is Nothing Then cn.Close()
End Try
End Function
我尝试了所有可能的解决方案,但它们没有起作用.任何帮助将不胜感激.
I tried all possible solutions and they didn't work. Any help would be appreciated.
推荐答案
Catch
块不返回值.将其更改为返回值的位置,例如:
The Catch
block doesn't return a value. Change it to where it returns a value, like so:
Public Function getkey(ByVal id As String)
Dim cmd As SqlCommand
Try
cmd = New SqlCommand("dbo.getkeydesc", GenLog.cn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@id", id)
Dim r As SqlDataReader = cmd.ExecuteReader()
If r.HasRows Then
Return True
Else
Return False
End If
Catch ex As Exception
MsgBox(ex.ToString)
Return False
Finally
' If Not cn Is Nothing Then cn.Close()
End Try
End Function
这篇关于函数不会在所有代码路径上返回值.使用结果时,运行时可能会出现空引用异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文