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

  • Question

  • Hi gurus,

    I have the following code to import an excel spreadsheet but I have the message that it doesn't return a value on all code paths & could return a null value. I can't spot where I need another return value perhaps? Please help point out what I'm missing. I know I could just suppress the message but that's not the way to go. This code courtesy of another developer's web site some time back.  I have added options I've researched to his code ie: Dim result. All help most graciously appreciated

        '   Courtesy of J.C.Solvoterra 
        Public Function ImportFromExcel(ByVal FilePath As String, ByVal isHDR As String) As DataTable
            Dim extension = IO.Path.GetExtension(FilePath)
            Dim conStr As String = Nothing
            Dim result = Nothing
    
            Try
    
                If IsFileOpen(FilePath) Then
                    If Not (MessageBox.Show("The File  " & Chr(34) & FilePath & Chr(34) & vbCrLf & "is Currently in use by another Application!" & vbCrLf & vbCrLf & "Please close the file then Click OK else Cancel", "File in Use", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1) = System.Windows.Forms.DialogResult.OK) Then
                        Exit Function
                    End If
                End If
    
                Select Case extension
                    Case ".xls"
                        'Excel 97-03
                        conStr = EXCEL97
                        Exit Select
                    Case ".xlsx"
                        'Excel 07
                        conStr = EXCEL07
                        Exit Select
                End Select
                conStr = String.Format(conStr, FilePath, isHDR)
    
                Dim connExcel As New OleDbConnection(conStr)
                Dim cmdExcel As New OleDbCommand()
                Dim oda As New OleDbDataAdapter()
                Dim table As New DataTable()
                cmdExcel.Connection = connExcel
                'Get the name of First Sheet
                connExcel.Open()
                Dim dtExcelSchema As New DataTable
                dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
                Dim SheetName As String = dtExcelSchema.Rows(0)("TABLE_NAME").ToString()
                connExcel.Close()
    
                'Read Data from First Sheet
                connExcel.Open()
                cmdExcel.CommandText = "SELECT * From [" & SheetName & "]"
                oda.SelectCommand = cmdExcel
                oda.Fill(table)
                connExcel.Close()
    
                result = table
                'Dim RV As ReportViewer = New ReportViewer
                'RV.LocalReport.DataSources.Add(New ReportDataSource)
                'RV.LocalReport.DataSources.Item(0).Value = dt
    
            Catch ex As Exception
    
            End Try
    
            Return result
    
        End Function

    Wednesday, February 5, 2020 9:57 AM

Answers

All replies

  • When you call 

    Exit Function

    It does not return any value...

    Wednesday, February 5, 2020 10:11 AM
  • Such a simple thing had me looking everywhere. I added Return Nothing to the line before Exit Function & problem solved. Many thanks
    Wednesday, February 5, 2020 10:20 AM
  • Hi,
    at first set OPTION STRICT ON and you see the problem.

    ' Courtesy of J.C.Solvoterra Public Function ImportFromExcel(ByVal FilePath As String, ByVal isHDR As String) As DataTable

    ' return type is DataTable ...

    Dim result = Nothing ' type of result is object! ...

              Exit Function ' return without setting a return value

    ...

    result = table ' only if no error the return type is DataTable

    ...

    Catch ex As Exception

    ' in this case result can be Nothing

    End Try Return result ' in case of error function return nothing End Function



    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks


    • Edited by Peter Fleischer Wednesday, February 5, 2020 10:56 AM correct comments for exit function
    Wednesday, February 5, 2020 10:29 AM
  • "Exit Function" (and "Exit Sub", etc.) are holdovers from VB6, only there for legacy reasons.

    It's much clearer to use "Return ...".   This way you know exactly what is being returned and when.


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

    Wednesday, February 5, 2020 3:06 PM