none
Function doesn't return a value on all code paths RRS feed

  • Question

  • Hi Gurus of VB.Net,

    I have the following function in a module & I get the green squiggly line which indicates that the function doesn't return a value on all code paths. Since it's an If...ElseIf I fail to understand why. Could someone tell me why & how I avoid that issue please

        Function sigpath() As String
            If Left$(SYSversion, 19) = "Microsoft Windows 7" Or Left$(SYSversion, 19) = "Microsoft Windows 8" Or Left$(SYSversion, 20) = "Microsoft Windows 10" Then
                sigpath = appdata & "\Microsoft\Signatures\" & My.Settings.EmailSignature
            ElseIf Left$(SYSversion, 20) = "Microsoft Windows XP" Then
                sigpath = "C:\Documents and Settings\" & Environ("username") &
                                "\Application Data\Microsoft\Signatures\" & My.Settings.EmailSignature
            End If
        End Function

    Friday, February 8, 2019 2:02 PM

Answers

  • Think of it this way.  First you have an If.  There is a return value set for it.  Second you have an ElseIf and there is a return value set for it , but what if the ElseIf is not true?  Then there is no path.  Simply change the ElseIf to If or provide an Else to the ElseIf to provide the missing path.

    Lloyd Sheen

    • Proposed as answer by leshay Friday, February 8, 2019 2:42 PM
    • Marked as answer by Hugh Self Taught Friday, February 8, 2019 4:05 PM
    Friday, February 8, 2019 2:34 PM
  • First of all, use the "Return" statement, not this VB6-era way of assigning to the function name.

    If you do this, then you don't have to do anything but add a 'catch-all' return at the end:

        Function sigpath() As String
            If Left$(SYSversion, 19) = "Microsoft Windows 7" Or Left$(SYSversion, 19) = "Microsoft Windows 8" Or Left$(SYSversion, 20) = "Microsoft Windows 10" Then
                Return appdata & "\Microsoft\Signatures\" & My.Settings.EmailSignature
            ElseIf Left$(SYSversion, 20) = "Microsoft Windows XP" Then
                Return "C:\Documents and Settings\" & Environ("username") &
                                "\Application Data\Microsoft\Signatures\" & My.Settings.EmailSignature
            End If
    
            'use an assertion or Stop here to let you know that nothing else was returned ...
    #If DEBUG
            Stop
    #End If
    
            Return ""
        End Function

    Get used to using assertions so that it can be brought to your attention when something doesn't go as planned.  VB & C# need a less-clumsy way of inserting these type of statements, but for now you have to use "#If DEBUG" to prevent it from affecting your users.


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter



    Friday, February 8, 2019 2:43 PM

All replies

  • Think of it this way.  First you have an If.  There is a return value set for it.  Second you have an ElseIf and there is a return value set for it , but what if the ElseIf is not true?  Then there is no path.  Simply change the ElseIf to If or provide an Else to the ElseIf to provide the missing path.

    Lloyd Sheen

    • Proposed as answer by leshay Friday, February 8, 2019 2:42 PM
    • Marked as answer by Hugh Self Taught Friday, February 8, 2019 4:05 PM
    Friday, February 8, 2019 2:34 PM
  • First of all, use the "Return" statement, not this VB6-era way of assigning to the function name.

    If you do this, then you don't have to do anything but add a 'catch-all' return at the end:

        Function sigpath() As String
            If Left$(SYSversion, 19) = "Microsoft Windows 7" Or Left$(SYSversion, 19) = "Microsoft Windows 8" Or Left$(SYSversion, 20) = "Microsoft Windows 10" Then
                Return appdata & "\Microsoft\Signatures\" & My.Settings.EmailSignature
            ElseIf Left$(SYSversion, 20) = "Microsoft Windows XP" Then
                Return "C:\Documents and Settings\" & Environ("username") &
                                "\Application Data\Microsoft\Signatures\" & My.Settings.EmailSignature
            End If
    
            'use an assertion or Stop here to let you know that nothing else was returned ...
    #If DEBUG
            Stop
    #End If
    
            Return ""
        End Function

    Get used to using assertions so that it can be brought to your attention when something doesn't go as planned.  VB & C# need a less-clumsy way of inserting these type of statements, but for now you have to use "#If DEBUG" to prevent it from affecting your users.


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter



    Friday, February 8, 2019 2:43 PM
  • Thanks guys! Both the answers From @Dave & @Lloyd led me down the path of correcting & understanding. After reading Lloyds answer I had assigned a variable to the statement then the sigpath to that instead of just assigning it to the return value which I gleaned from @Dave's reply.

    Much appreciated

    Friday, February 8, 2019 4:04 PM