[RESOLVED] Doesn't return value on all code paths
Hi There Guys,
I'm trying to get my head around this issue, on a few of my functions i get a warning " 'functionDealWithHiddenFields' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used."
Example function:
vb.net Code:
Function functionDealWithHiddenFields(ByVal HTML As String, ByVal numHiddenFields As Integer, ByVal X As Integer)
Try
'For i As Integer = 0 To numHiddenFields
'// The html source
Dim sText = HTML
'// The string we are searching for
Dim sPattern As String = "(?<=<input type=""hidden"" class="").*?(?="".*?')"
'// This is the regular expression
Dim regex As New Regex(sPattern, RegexOptions.IgnoreCase Or RegexOptions.Singleline)
'// The matches that we return are put in the matchcollection
Dim regexMatches As MatchCollection = regex.Matches(sText)
'// Return
'Return regexMatches(0).ToString
'MessageBox.Show("Hidden Field " & X & ": " & regexMatches(X).ToString)
Return regexMatches(X).ToString
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Function
The functions still work correctly (as far as i can tell) what would i need to do to the above function to remedy that warning?
thanks for any info guys
Graham
Re: Doesn't return value on all code paths
Its basically saying if you fall into the "Catch" block then your function doesn't return anything.
You don't need to fix it but if you don't want that message occurring all the time just put a line within the Catch block to return something (whatever is appropriate)
Re: Doesn't return value on all code paths
You have a Return in the Try portion of the block.... but not in your Catch... so if an exception does happen, the funtion will return a NULL (or Nothing) ... which could cause problems for the code that calls that function.
-tg
Re: Doesn't return value on all code paths
The cause has already been established. The pattern you have is this:
vb.net Code:
Try
'Do something.
Return someValue
Catch
End Try
To solve the issue you could do this:
vb.net Code:
Try
'Do something.
Return someValue
Catch
Return Nothing
End Try
but, in many people's eyes, including mine, it is more correct to do this:
vb.net Code:
Dim result = Nothing
Try
'Do something.
result = someValue
Catch
End Try
Return result
Having a single point at which your method always ends is considered good practice by many as it tends to be clearer.
Re: Doesn't return value on all code paths
Hi Thank you for the input, that has definately cleared things up :)
then check if (function is not nothing) to see if we got avalue returned would that be right?
thanks guys
Graham
Re: Doesn't return value on all code paths
Quote:
Originally Posted by
graham23s
Hi Thank you for the input, that has definately cleared things up :)
then check if (function is not nothing) to see if we got avalue returned would that be right?
thanks guys
Graham
Yes that's right.
I personally like jm's 3rd way in the sense that it is more logical to human understanding. When you just declare an object, it is nothing. After whatever processing you do you just return that object, whether it has or doesn't have anything. You don't need to deal with all those individual cases; really helpful in complex cases where there are multiple code paths. :)
Re: Doesn't return value on all code paths
Thanks a lot guys, as a rule of thumb i will do it like that in future :)
thanks again
Graham