How to eliminate warning that array is passed by reference before it has been assigned a value. RRS feed

  • Question

  • User646364117 posted

    Warning; In the line near the end 

    p.Executive(dsData, filePath, SheetNameArray) 

    I get a warning that sheet name array is being passed by reference before it has been assigned a value.

    How do i eliminate the warning?

     Protected Sub ImportExcel(sender As Object, e As EventArgs)
                Dim SheetCount As Integer
                Dim SheetNameArray() As String 
                Dim p As New Procedures 
                'Open the Excel file in Read Mode using OpenXml.
                Using doc As SpreadsheetDocument = SpreadsheetDocument.Open(filePath, False)
                    Dim sheets As Sheets = doc.WorkbookPart.Workbook.Sheets
                    SheetCount = 1 'Added bySG
                    For Each sheet As Sheet In sheets
                        sheetsCollection.Add(sheet.Id.Value, sheet.Name)
                        SheetCount += 1 'added
                        ReDim Preserve SheetNameArray(SheetCount) 
                        SheetNameArray(SheetCount) = sheet.Name 
    Next 'IRRELEVANT CODE DELETED p.Executive(dsData, filePath, SheetNameArray) 'Stubbed Out End Sub

    Saturday, September 10, 2016 1:03 PM


  • User1724605321 posted

    Hi sg48asp,

    The error is clear ,if you know the array size , you could try declare and instantiate arrays like :

    Dim SheetNameArray(5) As String

    If you are going to use a collection that you don't know the size of in advance, there are better options than arrays. You could use a List<string> instead - it will allow you to add as many items as you need .

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 12, 2016 6:25 AM