功能返回问题?

时间:2014-02-17 16:21:28

标签: vb.net visual-studio-2010 .net-4.0

在我的VB.Net程序中,我发现有两(2)个警告显示以下消息:

函数不会在所有代码路径上返回值。使用结果时,可能会在运行时发生空引用异常

帮助我解决问题并解决问题。以下快照是警告错误指示的位置:

'结束功能'附近的冷杉警告

'Executes SQL commands to the system database
Public Function ExecSQL(ByVal sql As String)
        Dim com As New MySqlCommand(sql)
        Try
            RefreshConnection()
            com.Connection = con
            com.ExecuteNonQuery()
        Catch ex As Exception
            Return ex
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
        End Try
End Function

'End Function'附近的第二个Waring

'Get the value of an specific field in a given sql string
    Public Function GetField(ByVal sql As String, ByVal field As String)
        Try
            RefreshConnection()
            Dim com As New MySqlCommand(sql, con)
            Dim dReader As MySqlDataReader = com.ExecuteReader

            While dReader.Read
                GetField = dReader(field).ToString
            End While
            dReader.Close()
        Catch ex As Exception
            Return ex
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
        End Try
    End Function

2 个答案:

答案 0 :(得分:0)

这是因为在异常处理程序中实际返回异常对象ex。如果没有异常发生,你不会返回任何内容。您可以在Return Nothing之上明确添加Catch ex as Exception,但我想从VB中自动为您做的消息......

顺便说一句:将异常返回给调用者有什么意义?将消息框放入调用方法并将调用包含在Try-Catch中。显然不打算与用户交互的方法根本不应显示消息。然后你可以直接传递异常,直到你结束表示层。

长话短说:业务逻辑中没有消息框。用户信息仅在表示层中。


读完第二种方法后:情况更糟糕了!如果数据读取器有行,则将方法的结果设置为您找到的最后一个(!)结果(这本身就是WTF,但是另一个故事)。如果发生错误,结果是Exception类型的对象,因此调用代码甚至需要确定函数调用的结果实际上是字段值还是异常!

我的朋友设计真的很糟糕。


您要求输入固定代码:

'Executes SQL commands to the system database
Public Sub ExecSQL(ByVal sql As String)
    Dim com As New MySqlCommand(sql)

    RefreshConnection()
    com.Connection = con
    com.ExecuteNonQuery()
End Sub

'Get the value of an specific field in a given sql string
Public Function GetField(ByVal sql As String, ByVal field As String)
    RefreshConnection()
    Dim com As New MySqlCommand(sql, con)
    Dim dReader As MySqlDataReader = com.ExecuteReader

    GetField = Nothing

    While dReader.Read
        GetField = dReader(field).ToString
    End While
    dReader.Close()
End Function

答案 1 :(得分:0)

这里有几个缺陷。为了简洁起见,我将发布一些改进的代码:

'Executes SQL commands to the system database
Private Function ExecSQL(ByVal sql As String, ByVal params As IEnumerable(Of MySqlParameter))
    Using cn As New MySqlConnection(GetConnectionString()), _
          com As New MySqlCommand(sql, cn)

        For Each param As MySqlParameter in params
           com.Parameters.Add(param)
        Next param
        cn.Open()
        com.ExecuteNonQuery()
    End Using
End Function

'Get the value of an specific field in a given sql string
Private Function GetField(Of T)(ByVal sql As String, ByVal params As IEnumerable(Of MySqlParameter), ByVal field As String) As T
     Using cn As New MySqlConnection(GetConnectionString())
           com As New MySqlCommand(sql, cn)

         For Each param As MySqlParameter In params
            com.Parameters.Add(param)
         Next param
         cn.Open()

         Using rdr As MySqlDataReader = com.ExecuteReader()
             While rdr.Read()
                 Return CType(rdr(field), T)
             End While
         End Using
    End Using
    Return Nothing
End Function