Windows Phone Developers

Sunday, April 27, 2008

Function '' doesn't return a value on all code paths


Function '' doesn't return a value on all code paths


The error Function '' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used occurs when a Function procedure has at least one possible path through its code that does not return a value.

You can return a value from a Function procedure in any of the following ways:

· Assign the value to the Function procedure name and then perform an Exit Function statement.

· Assign the value to the Function procedure name and then perform the End Function statement.

· Include the value in a Return Statement (Visual Basic).

If control passes to Exit Function or End Function and you have not assigned any value to the procedure name, the procedure returns the default value of the return data type.

Error ID: BC42105

To correct this error

· Check your control flow logic and make sure you assign a value before every statement that causes a return.

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.

Most often this kind of error happens when the return is missed in the Catch block. It would be advisable to return a value for the error too as shown below

Function ReturnSomeVal() As String

Try

Dim Svalue As String

' Processing

Return sValue

Catch ex As Exception

Return vbNullString

Finally

End Try

End Function

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

2 comments:

  1. Thanks - a simple, but effective tip.
    What about using "Return Nothing" as well - in case your function returns something besides a string value?

    ReplyDelete
  2. Thanks a lot Dude...

    ReplyDelete