Odd warning: doesn't return a value on all code paths.

G

Guest

I have the function below. it returns a "simpleresult" which I've also
included the definition of below.

In VS2005 (after upgrading the project), I get a warning indicating that
Function 'CloseVantive' doesn't return a value on all code paths. A null
reference exception could occur at run time when the result is used.

The code runs fine. But why do I get this message? How do i get rid of it?

The only thing I found is if i add a line at the start of the function that
says:
CloseVantive=New SimpleResult but I don't see why that would be necessary.

Function CloseVantive(ByVal connection As Integer) As SimpleResult
Dim vanresult As Integer

Try
If (connection > 0) Then
IncrementRequestCounterNamed("Vantive Connections Closed")

vanresult = VanCloseConnection(connection, 0, 0)
If (vanresult < 0) Then
CloseVantive.ResultDescription = "VanAPI Error# " +
vanresult.ToString + " (" + TranslateVANError(vanresult) + ") - when closing
connection (CloseVantive)."
CloseVantive.APIResult = vanresult
End If
End If
Catch e As Exception
Throw New Exception("Error in CloseVantive.", e)
End Try
End Function


Public Structure SimpleResult
Dim APIResult As Integer ' 0 = success. <0 indicates
error.
Dim ResultDescription As String ' If error, text description of
error.
End Structure
 
T

Terry Olsen

Try this...


Function CloseVantive(ByVal connection As Integer) As SimpleResult
Dim rslt as SimpleResult
Dim vanresult As Integer

Try
If (connection > 0) Then
IncrementRequestCounterNamed("Vantive Connections Closed")
vanresult = VanCloseConnection(connection, 0, 0)
If (vanresult < 0) Then rslt.ResultDescription = _
"VanAPI Error# " + _
vanresult.ToString + _
"
(" + _
TranslateVANError(vanresult) + _
") - when closing connection (CloseVantive)."
rslt.APIResult=vanresult
End If
End If
Catch e As Exception
Throw New Exception("Error in CloseVantive.", e)
End Try
Return rslt
End Function
 
C

Chris Dunaway

Robert said:
In VS2005 (after upgrading the project), I get a warning indicating that
Function 'CloseVantive' doesn't return a value on all code paths. A null
reference exception could occur at run time when the result is used.

The code runs fine. But why do I get this message? How do i get rid of it?

The warning is very clear, it is possible that your function does not
return a result in some circumstances. For example, what if
'connection' is not greater than 0? What if vanresult >= 0? In these
cases, your function does not return a value, hence the warning!

To get rid of the warning, make sure that all paths through the
function will eventually return a result.
 
H

Herfried K. Wagner [MVP]

Chris,

Chris Dunaway said:
The warning is very clear, it is possible that your function does not
return a result in some circumstances. For example, what if
'connection' is not greater than 0? What if vanresult >= 0? In these
cases, your function does not return a value, hence the warning!

In this particular case the function returns 'Nothing', which is an "empty"
instance of the structure 'SimpleResult'.
To get rid of the warning, make sure that all paths through the
function will eventually return a result.

The function always returns a value, even if the return value is not
explicitly assigned/returned.
 
G

Guest

The warning is useful because if there is no explicit function name
assignment or explicit 'Return' statement, then it is highly likely that the
programmer simply forgot to do this. Your code has neither explicit function
name assignment nor return statement (you are assigning to a member of the
implicit function name value, but not the function name itself). If I were
reviewing your code, I would also say that you "forgot" to either assign to
the function name or explicitly 'Return'.

The code compiles fine because VB will always assume you are returning the
implicit value associated with the function name if all else fails, but why
not be a little more direct about your intention? If nothing else, at least
for maintenance purposes.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant C++: C# to C++ Converter
Instant J#: VB.NET to J# Converter
 
C

Crouchie1998

'VanCloseConnection' isn't declared from what I can see

Crouchie1998
BA (HONS) MCP MCSE
 
C

Chris Dunaway

Herfried said:
The function always returns a value, even if the return value is not
explicitly assigned/returned.

Yes, but does the warning take that into account? Or does it mean
there are code paths which do not explicitly return a SimpleResult
structure?
 
H

Herfried K. Wagner [MVP]

Chris Dunaway said:
Yes, but does the warning take that into account?

No, it doesn't.
Or does it mean there are code paths which do not explicitly
return a SimpleResult structure?

That's what it's actually meaning.
 

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