Results 1 to 7 of 7

Thread: [RESOLVED] Function '' doesn't return a value on all code paths.

  1. #1

    Thread Starter
    Addicted Member ashveen's Avatar
    Join Date
    Sep 2013
    Location
    Sri Lanka
    Posts
    141

    Resolved [RESOLVED] Function '' doesn't return a value on all code paths.

    Code:
        Private Function RequiredEntry() As Boolean
            If txtCakeID.Text = "" Or txtName.Text = "" Or txtCake_Description.Text = "" Or txtWeight.Text = "" Or txtPrice.Text = "" = True Then
                MsgBox("Please enter required * information....", MsgBoxStyle.Critical, "Attention...")
                Return True
                Exit Function
            End If
        End Function
    Can someone please tell me why do I get this warning.

    "Function 'RequiredEntry' doesn't return a value on all code paths."

    And also in the above code "End Function" is underlined.

    The program works fine without any errors but I want to make it perfect by fixing this warning error.

    Thank You!

  2. #2
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,757

    Re: Function '' doesn't return a value on all code paths.

    You get the warning because the function doesn't return a value on all code paths just as it says. When your if statement result in true, the code encounters a Return statement, However when the if statement results in false, there is no Return statement encountered.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Function '' doesn't return a value on all code paths.

    That's why I'm not a fan of returns in the middle of functions. I'm one of those types that subscribes to the Single Entry Single Exit methodology... But, there are sometimes when it's appropriate to shuck that and do what's needed.

    At any rate... there's two ways to solve it... one is to provide a return on the alternate path.
    Code:
    Private Function RequiredEntry() As Boolean
            If txtCakeID.Text = "" Or txtName.Text = "" Or txtCake_Description.Text = "" Or txtWeight.Text = "" Or txtPrice.Text = "" = True Then
                MsgBox("Please enter required * information....", MsgBoxStyle.Critical, "Attention...")
                Return True
                Exit Function
            End If
          Return False ' Presumably since your If returns true, the alternate returns false
        End Function
    I call this the pepper method, your function gets peppered with returns.


    The other is to use the single exit. I usually start all my functions off the same way:
    Code:
    '(Stub)
    Private Function SomeFunction() as Boolean
      Dim retValue as Boolean = False ' Default value
    
      Return retValue
    End function
    Then I flesh it out.
    so yours would look like this:
    Code:
    Private Function RequiredEntry() As Boolean
        Dim retValue as Boolean = False
        If txtCakeID.Text = "" Or txtName.Text = "" Or txtCake_Description.Text = "" Or txtWeight.Text = "" Or txtPrice.Text = "" = True Then
            MsgBox("Please enter required * information....", MsgBoxStyle.Critical, "Attention...")
            retValue = True
        End If
        Return retValue    
    End Function
    Now I'm guaranteed to ensure a value gets returned along all code paths.

    Again, this is really just my design preference. Some people swear by the former while others will swear by the latter. I've actually seen problems with both. Main key I think is to be consistent.


    -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
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,757

    Re: Function '' doesn't return a value on all code paths.

    The other is to use the single exit. I usually start all my functions off the same way:
    That's how I do it as well.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  5. #5

    Thread Starter
    Addicted Member ashveen's Avatar
    Join Date
    Sep 2013
    Location
    Sri Lanka
    Posts
    141

    Re: Function '' doesn't return a value on all code paths.

    thanks a lot Kebo and Techgnome. It worked.

  6. #6
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    710

    Re: [RESOLVED] Function '' doesn't return a value on all code paths.

    Another thing is that you should never need an "Exit Function". If you've used the "Return" statement, that's all that's needed.
    The only time "Exit Function" ever makes sense is if you're using the old "function name as a temp variable" scheme, which is absolutely a bad idea now that "Return" is available (since VB.NET version 1).
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com

  7. #7
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: [RESOLVED] Function '' doesn't return a value on all code paths.

    I am going to nit pick at every Ones examples here. For One no messagebox should really be used here. It's a validation function. Thats it's task. Next there is comparing booleans to true or false. RETURN the orelse statement.

    Code:
    Dim isvalid = txtCakeID.Text = "" Or txtName.Text = "" Or txtCake_Description.Text = "" Or txtWeight.Text = "" Or txtPrice.Text = ""
    Even add some LINQ

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    4.         If RequiredEntry() Then
    5.             ' passed
    6.         Else
    7.             MessageBox.Show("Invalid!!!")
    8.         End If
    9.     End Sub
    10.  
    11.     Private Function RequiredEntry() As Boolean
    12.         Return Not {Me.TextBox1, Me.TextBox2, Me.TextBox3}.Any(Function(tb) tb.Text.Trim = String.Empty)
    13.     End Function
    14. End Class

Tags for this Thread

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