Function '
The error Function '
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
Thanks - a simple, but effective tip.
ReplyDeleteWhat about using "Return Nothing" as well - in case your function returns something besides a string value?
Thanks a lot Dude...
ReplyDelete