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 ステートメントがあるにもかかわらず、関数がすべてのコード パスで値を返さないと表示されます。上記のエラーは表示されません。

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 に答える