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:
Quote:
|
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:
Code:
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:
Quote:
|
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,
Code:
result Color = Nothing
I could return nothing, but finally had to set:
Code:
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
like:
Code:
Try
'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:
Code:
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
Try
'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.