Results 1 to 4 of 4

Thread: Function does not return value in all code paths error

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2018
    Posts
    276

    Function does not return value in all code paths error

    Hi,
    I am facing "issue" "Function does not return value in all code paths" when clicking on a button. It is not really an issue since it works fine within app itself. The problem is when executing app via play button - I have got a lot of a first chance exception occured in.... I have tried resolve it via Return statement - https://www.vbforums.com/showthread....=1#post3293950, but my function does not contains IF condition.
    Please dont forget to add good reputation if my advices were useful for you.
    How? Under this post there is "RATE THIS POST" button. Click on it.

  2. #2
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,473

    Re: Function does not return value in all code paths error

    Quote Originally Posted by VB.NET Developer View Post
    Hi,
    I am facing "issue" "Function does not return value in all code paths" when clicking on a button. It is not really an issue since it works fine within app itself. The problem is when executing app via play button - I have got a lot of a first chance exception occured in.... I have tried resolve it via Return statement - https://www.vbforums.com/showthread....=1#post3293950, but my function does not contains IF condition.
    Could you post the function in question?

    It doesn't always have to be an If statement that causes this, any time there is more than one possible way to exit a function (other than throwing an exception) all of the possible ways need to return something. If you have an If, a select case, or just don't have a return statement at the end you can get this error.

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,753

    Re: Function does not return value in all code paths error

    Here are two examples of when the exception would occur:
    Code:
    Private Function Foo(input As String) As String
        If (input = "foo") Then
            Return "hello world"
        End If
    End Function
    
    Private Function Bar() As String
        Select Case DateTime.Now.Minute
            Case 0
                Return "Hello World
        End Select
    End Function
    In the first instance, if the input parameter is not "foo" then the function will not return a value. In the second instance, if the current minute is not 0 then the function will not return a value.

    If you require a value or require that a value have a particular value, then you should throw an exception (or return a default value) when the condition isn't met. For example, let's use a real life scenario where I have a dictionary that holds an item off a restaurant menu as its key and the respective price as its value. If I were creating a function to get the price and then apply sales tax to it, then I would throw an exception if the value passed to the function is not in the menu:
    Code:
    Private Function GetTotalCost(item As String) As Double
        If (String.IsNullOrWhiteSpace(item)) Then
            Throw New ArgumentNullException("item")
        End If
        If (Not _menu.ContainsKey(item)) Then
            Throw New ArgumentException(item & " is not a valid menu item.")
        End If
    
        Dim itemCost As Double = _menu(item)
        Dim applicableTax As Double = itemCost * _salesTax
    
        Return itemCost * applicableTax
    End Function
    Fiddle: https://dotnetfiddle.net/sr0ASS

    The incorrect way to write this function that would generate the error in question would be:
    Code:
    Private Function GetTotalCost(item As String) As Double
        If (Not String.IsNullOrWhiteSpace(item) AndAlso _menu.ContainsKey(item)) Then
            Dim itemCost As Double = _menu(item)
            Dim applicableTax As Double = itemCost * _salesTax
            Return itemCost * applicableTax
        End If
    End Function
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: Function does not return value in all code paths error

    Quote Originally Posted by VB.NET Developer View Post
    The problem is when executing app via play button - I have got a lot of a first chance exception occured in....
    I find this interesting. That shouldn't be throwing an exception. That warning is just that, a warning.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

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