'<procedurename>' doesn't return a value on all code paths?

  • Thread starter Thread starter Andy B.
  • Start date Start date
A

Andy B.

I have the function below. I am confused because I have created ones like
this lots of times and never ran into this error before. Is there any way to
fix it? I am trying to return results from an ObjectQuery.

Public Function GetNewsArticles(ByVal Type As String) As ObjectQuery(Of
NewsArticles)

If (Type = "Archive") Then

Return DBContext.NewsArticles.Where("it.Archived =
true").OrderBy("it.CreationDate desc")

End If

If (Type = "Current") Then

Return DBContext.NewsArticles.Where("it.Archived =
false").OrderBy("it.CreationDate desc")

End If

End Function
 
Andy B. said:
I have the function below. I am confused because I have created ones like
this lots of times and never ran into this error before. Is there any way
to fix it? I am trying to return results from an ObjectQuery.

Public Function GetNewsArticles(ByVal Type As String) As ObjectQuery(Of
NewsArticles)

If (Type = "Archive") Then

Return DBContext.NewsArticles.Where("it.Archived =
true").OrderBy("it.CreationDate desc")

End If

If (Type = "Current") Then

Return DBContext.NewsArticles.Where("it.Archived =
false").OrderBy("it.CreationDate desc")

End If

End Function

Compiler is just telling you that if the value of Type is not "Archive" or
"Current" then your function will not return anything. A suggestion for
this program construct is to use a case statement:

Select case Type
case "Archive"
return ...
case "Current"
return ....
case else
return nothing (or error since you have no code for this path)
End Select

LS
 
Andy

The best practices guide ( yes it exists ) tells you to return values at the
end of the method and not to break out of methods unless you really need to
so in short declare a return parmeter at the top set it in your code
construct and return it at the end

HTH

Michel
 
Andy B. said:
I have the function below. I am confused because I have created ones like
this lots of times and never ran into this error before. Is there any way
to fix it? I am trying to return results from an ObjectQuery.

Public Function GetNewsArticles(ByVal Type As String) As ObjectQuery(Of
NewsArticles)

If (Type = "Archive") Then

Return DBContext.NewsArticles.Where("it.Archived =
true").OrderBy("it.CreationDate desc")

End If

If (Type = "Current") Then

Return DBContext.NewsArticles.Where("it.Archived =
false").OrderBy("it.CreationDate desc")

End If

\\\
Return Nothing
///
 
Got this to work now. All I had to do in my select case statement is put
case else return nothing
 
Back
Top