473,422 Members | 1,768 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,422 software developers and data experts.

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

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
7 11600
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
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
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
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
'VanCloseConnection' isn't declared from what I can see

Crouchie1998
BA (HONS) MCP MCSE
Nov 23 '05 #6
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Dylan | last post by:
Just wondered what you guys do when you need a method that returns a const reference to some data based upon some value. something like this: const CData& MyClass::GetDataWithValue(int...
29
by: junky_fellow | last post by:
Consider the following piece of code: struct junk { int i_val; int i_val1; char c_val; }; int main(void) {
3
by: Bas Wassink | last post by:
Hello there, I'm having trouble understanding a warning produced by 'splint', a code-checker. The warning produced is: keywords.c: (in function keyw_get_string) keywords.c:60:31: Released...
5
by: Dmitriy Lapshin [C# / .NET MVP] | last post by:
Hi all, I think the VB .NET compiler should at least issue a warning when a function does not return value. C# and C++ compilers treat this situation as an error and I believe this is the right...
7
by: Military Smurf | last post by:
I have a very simple function I'm working on. Function CarSpeed(ByVal kph As Integer) If kph < 0 Then _quote = "the car is in reverse" _actualspeed = kph ElseIf kph = 0 Then _quote = "the car...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
4
by: a | last post by:
I have been cleaning up somebody else's code in Vs2005 .net 2.0 There wer lots of these code pat errors because of unitialized variables in functions with Conditionals - that may or may not be...
27
by: Terry | last post by:
I am getting the following warning for the below function. I understand what it means but how do I handle a null reference? Then how do I pass the resulting value? Regards Warning 1...
3
by: Andy Fish | last post by:
The following code: enum Foo { one, two }; int Bar(Foo foo) { switch (foo) { case Foo.one: return 7; case Foo.two:
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.