angelogogo angelogogo - 4 months ago 12
Vb.net Question

LINQ function to return list but compiler says function doesn't return a value on all code path

Here is my code

<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


but when I compile it says that the function doesn't return a value on all code path despite having a return statement on all of my if statement am I missing something and also I notice when there if no elseif statement just the usual if else end it doesn't give me an error stated above.

Answer Source

All code paths don't return a value.

 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.

You could also just put an else at the end of the if-else which would probably be better. I didn't notice the ElseIf's at fist cause I don't write that VB stuff.