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

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
 
L

Lloyd Sheen

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
 
M

Michel Posseth [MCP]

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
 
H

Herfried K. Wagner [MVP]

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
///
 
A

Andy B.

Got this to work now. All I had to do in my select case statement is put
case else return nothing
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top