1

这是我的代码

<WebMethod()> _
    Public Function getlocationsbypro(searchtype As String, crime As String, proid As String, startdate As String, enddate As String, starttime As String, endtime As String) As List(Of crimelocation)

    If searchtype = "withdatetime" Then
        Dim locs As New crimemsapslocationDataContext
        Dim giveloc = From locations In locs.crimelocations _
                      Where locations.INCIDENTTYPE = crime And (locations.DATE_COMTD >= Convert.ToDateTime(startdate) And locations.DATE_COMTD <= Convert.ToDateTime(enddate)) _
                      And (locations.gettimecom >= starttime And locations.gettimecom <= endtime) _
                      Select locations
        Return giveloc.ToList
    ElseIf searchtype = "withdate" Then
        Dim locs As New crimemsapslocationDataContext
        Dim giveloc = From locations In locs.crimelocations _
                      Where locations.INCIDENTTYPE = crime And (locations.DATE_COMTD >= Convert.ToDateTime(startdate) And locations.DATE_COMTD <= Convert.ToDateTime(enddate)) _
                      Select locations

        Return giveloc.ToList
    ElseIf searchtype = "without" Then
        Dim locs As New crimemsapslocationDataContext
        Dim giveloc = From locations In locs.crimelocations _
                      Where locations.INCIDENTTYPE = crime _
                      Select locations

        Return giveloc.ToList
    End If

End Function

但是当我编译它时它说该函数没有在所有代码路径上返回一个值,尽管我的所有 if 语句都有一个 return 语句我错过了什么,而且我注意到如果没有 elseif 语句只是通常的 if else end它没有给我上述错误。

4

2 回答 2

1

所有代码路径都不返回值。

 If searchtype = "withdatetime" Then
        Dim locs As New crimemsapslocationDataContext
        Dim giveloc = From locations In locs.crimelocations _
                      Where locations.INCIDENTTYPE = crime And (locations.DATE_COMTD >= Convert.ToDateTime(startdate) And locations.DATE_COMTD <= Convert.ToDateTime(enddate)) _
                      And (locations.gettimecom >= starttime And locations.gettimecom <= endtime) _
                      Select locations
        Return giveloc.ToList
    ElseIf searchtype = "withdate" Then
        Dim locs As New crimemsapslocationDataContext
        Dim giveloc = From locations In locs.crimelocations _
                      Where locations.INCIDENTTYPE = crime And (locations.DATE_COMTD >= Convert.ToDateTime(startdate) And locations.DATE_COMTD <= Convert.ToDateTime(enddate)) _
                      Select locations

        Return giveloc.ToList
    ElseIf searchtype = "without" Then
        Dim locs As New crimemsapslocationDataContext
        Dim giveloc = From locations In locs.crimelocations _
                      Where locations.INCIDENTTYPE = crime _
                      Select locations

        Return giveloc.ToList
    End If
 // if none of the if-else conditions are met your code goes straight to here and there is not return statement
    Return null; // this fixes it.

您也可以else在 if-else 的末尾加上一个,这可能会更好。我没有注意到 ElseIf 的出现,因为我不写那些 VB 的东西。

于 2013-06-15T03:48:28.063 回答
1

您需要在 End If 之后有另一个 Return 语句。否则,如果没有满足 If 或 ElseIf 条件,您的代码将不会返回任何内容。

于 2013-06-15T03:48:44.433 回答