Results 1 to 7 of 7

Thread: [RESOLVED] Doesn't return value on all code paths

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Location
    Scotland
    Posts
    417

    Resolved [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:
    1. Function functionDealWithHiddenFields(ByVal HTML As String, ByVal numHiddenFields As Integer, ByVal X As Integer)
    2.  
    3.         Try
    4.  
    5.             'For i As Integer = 0 To numHiddenFields
    6.             '// The html source
    7.             Dim sText = HTML
    8.  
    9.             '// The string we are searching for
    10.             Dim sPattern As String = "(?<=<input type=""hidden"" class="").*?(?="".*?')"
    11.  
    12.             '// This is the regular expression
    13.             Dim regex As New Regex(sPattern, RegexOptions.IgnoreCase Or RegexOptions.Singleline)
    14.  
    15.             '// The matches that we return are put in the matchcollection
    16.             Dim regexMatches As MatchCollection = regex.Matches(sText)
    17.  
    18.             '// Return
    19.             'Return regexMatches(0).ToString
    20.             'MessageBox.Show("Hidden Field " & X & ": " & regexMatches(X).ToString)
    21.  
    22.             Return regexMatches(X).ToString
    23.  
    24.         Catch ex As Exception
    25.  
    26.             MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    27.  
    28.         End Try
    29.  
    30.     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

  2. #2
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    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)

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Doesn't return value on all code paths

    The cause has already been established. The pattern you have is this:
    vb.net Code:
    1. Try
    2.     'Do something.
    3.  
    4.     Return someValue
    5. Catch
    6.  
    7. End Try
    To solve the issue you could do this:
    vb.net Code:
    1. Try
    2.     'Do something.
    3.  
    4.     Return someValue
    5. Catch
    6.     Return Nothing
    7. End Try
    but, in many people's eyes, including mine, it is more correct to do this:
    vb.net Code:
    1. Dim result = Nothing
    2.  
    3. Try
    4.     'Do something.
    5.  
    6.     result = someValue
    7. Catch
    8.  
    9. End Try
    10.  
    11. Return result
    Having a single point at which your method always ends is considered good practice by many as it tends to be clearer.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Location
    Scotland
    Posts
    417

    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

  6. #6
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Doesn't return value on all code paths

    Quote Originally Posted by graham23s View Post
    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.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Location
    Scotland
    Posts
    417

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width