这是我的代码
<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语句只是通常如果结束它不会给我上面说的错误。
答案 0 :(得分: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.
你也可以在if-else的末尾放一个else
,这可能会更好。我没有注意到ElseIf,因为我没有写那些VB的东西。
答案 1 :(得分:1)
在结束If后你需要另一个Return语句。否则,如果没有满足If或ElseIf条件,则代码将不返回任何内容。