By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,301 Members | 1,766 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,301 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Nov 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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
"Robert" <ro*****@noemail.nospam> wrote in message
news:E8**********************************@microsof t.com...
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

Nov 23 '05 #2

P: n/a
Robert wrote:
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.

Nov 23 '05 #3

P: n/a
Chris,

"Chris Dunaway" <du******@gmail.com> schrieb:
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!


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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 23 '05 #4

P: n/a
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

"Robert" wrote:
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

Nov 23 '05 #5

P: n/a
'VanCloseConnection' isn't declared from what I can see

Crouchie1998
BA (HONS) MCP MCSE
Nov 23 '05 #6

P: n/a
Herfried K. Wagner [MVP] wrote:
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.


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?

Nov 23 '05 #7

P: n/a
"Chris Dunaway" <du******@gmail.com> schrieb:
> 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.


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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.