Go Back  Xtreme Visual Basic Talk > General Discussion > Tech Discussions > obnoxious error of the week: function doesn't return value on all code paths

Thread Tools Display Modes
Old 04-14-2012, 07:09 PM
surfR2911 surfR2911 is offline
Join Date: Oct 2009
Posts: 719
Default obnoxious error of the week: function doesn't return value on all code paths

Yes it's VB.Net newbie time again.

I have never in my life written a function in VB.Net.

But unfortunately I spend a lot of time hunting down
code for different "niche" WinForms functions.
that PlausiblyDamp and AtmaWeapon think I should
be doing in WPF instead.
(Note: I respect their opinions but its about my choosing the right
"learning experience" for me).

The thing is when I do find something interesting code
that looks like it would dovetail into what I am trying to do
I get the all-too-common error:
"function doesn't return value on all code paths".

Yes I know why this error comes up.

It's because even though
the function does usually return one or more results
(depending on one or more value assignments
usually tucked inside one or more If..Then..End If statements),
the .Net IDE insists on wanting the function to
always return a result
(even if none of the If..End code actually assigns a result).

The IDE is worried about the code throwing unhandled
runtime exception errors but I really don't care when
I'm still in the middle of fleshing out the code.

If "function doesn't return value on all code paths" was
just a warning I would just ignore it but I can't,
because as an actual error the project won't build or run
until its cleared.

Here's the rather unhelpful MSDN documentation on the error:
"Function '<procedurename>' doesn't return a value on all code paths"
..but it does say at the end:
It is easier to guarantee that every return from the procedure returns a value if you always use the Return statement. If you do this, the last statement before End Function should be a Return statement.
So what I've been doing is adding the following
lines before the "End Function" line:
return = 0 'for returning integer types
return = vbNullString 'for returning string
return = Nothing 'or Null, for returning most other value types

This works most of the time except when
the function's return type is something weird like:
Function ColorBlendFoo (Dim (ByVal FromColor As Color, _
     ByVal ToColor As Color, _
       Optional ByVal alpha As Integer = 127) As Color
              'some per pixel calculations happens here
End Function
..in which case "Color" is some strange new VB.Net structure:
In Visual Basic 6.0, colors were represented by a value of type Long; in Visual Basic .NET colors are of type System.Drawing.Color.
..from "MSDN: Color Changes in Visual Basic .NET"

I tried coercing this .net color structure
directly into nothing,
result Color = Nothing
I could return nothing, but finally had to set:
result = color.empty
I found this out by using the IDE intellisense
so the IDE knows what the empty value default
result should be it just won't give to the suggestion
of using it to clear the error
--is that stupid or what?
(instead, in terms of clearing error suggestions about things
it seems clueless about, it will give you the option
of creating some special "stub" for a property, event
that just complicates things if you are dumb enough
to take the suggestion).

But this got me thinking..
maybe there is some way to use Try..Catch to
have a boilerplate template way of dealing with the error
        'Do something --evaluate a TryStatement
        Return someValue
    Catch ex As Exception
        Return Nothing
    End Try
..but I find I get better results with
code that goes like:
Public Function Foo(ByVal param1 As Something) As SomeValueType 
  'Depending on what "SomeValueType" is some coercing may be necessary..  
  'For the line below
  Dim result = Nothing 
  'Note: You can put the try before dimming/assignment of some variables
  'when the function does not contain 
  'a proper "trystatement" to evaluate
        'Do something --evaluate a TryStatement
        result = someValue
    Catch ex As Exception
    End Try
    Return result
What constitutes a proper TryStatement (you might ask)?

Anything that meets the "TryStatements Property"
part of the "CodeTryCatchFinallyStatement" class
part of the System.CodeDom namespace.

Now aren't you glad you asked?
Hint: I've run into a few
If..Then..Else statements that don't qualify
as proper TryStatements, that's why I included
the hint about using a Dim statement just to meet the criteria.

If this info anywhere on the XVBT forum - I didn't find it.

This actually comes from off forum thread.

Well that's about it for the "discussion"...
(I know this is too "lightweight" to meet the criteria
for going in Tutor's Corner),
but hopefully it comes up when someone is searching for
the error in the title of this thread.

So unless anyone has a suggestion for making the
IDE manually downgrade the error to a warning
or somehow "patch" the VB.Net 2010 Express Edition IDE
such that it can be smart enough to offer a default return
to clear the error with extra code -- I don't know what
else can be said on the topic.

Last edited by surfR2911; 04-14-2012 at 07:32 PM.
Reply With Quote
Old 04-14-2012, 08:59 PM
Gruff's Avatar
Gruff Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
Join Date: Aug 2003
Location: Oregon, USA
Posts: 6,440

Sounds like you got them ole VB6 blues again.

Please take this in the positive spirit in which it was written.

Many VB6 programmers including myself found VB.net differences hard to accept.
This is not an unusual response. I wouldn't waste time with all these work arounds.

The error serves a very good purpose.
Without it you could be looking at hours of debugging for a run-time error.

The simple answer is to not call the function if it not complete.
do not return early.

Private Function GetColor() as Color Dim OutColor as Color = Color.Red 'Or nothing ' Now no matter the path is taken a value will be returned. Develop away. If ... OutColor = someValue If ... OutColor = someValue If ... OutColor = someValue ... Return OutColor End Function
Burn the land and boil the sea
You can't take the sky from me


Last edited by Gruff; 04-14-2012 at 09:05 PM.
Reply With Quote
Old 04-15-2012, 09:25 PM
surfR2911 surfR2911 is offline
Join Date: Oct 2009
Posts: 719
Default Error even if function not called..

Sounds like you got them ole VB6 blues again
Always..but I definitely am committed to learning VB.Net now..
The error serves a very good purpose.
Without it you could be looking at hours of debugging for a run-time error.
Currently I spend more hours fighting the IDE (and trying to figure out what it wants) thanb debugging runtime errors, but maybe it will get better in the future..
The simple answer is to not call the function if it not complete.
Even if the function is not called the error still comes up during the build attempt, but thanks for the code snippet.
Reply With Quote
Old 04-16-2012, 11:42 AM
PlausiblyDamp's Avatar
PlausiblyDamp PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,005

Not sure about the Express edition but on the professional version you can change these from the compile tab of the property page.

Personally though I would class a function that doesn't always return a value as a bug and having it treated as an error seems the best approach (it is also how C# does it which means I don't need to remember two sets of rules)
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..