在VB.net中启动动态多维数组

时间:2014-08-01 18:00:56

标签: asp.net arrays vb.net

我收到一个未设置为对象实例的" Object引用。"尝试填充fileDetails数组时出错。我是vb.net的新手,我迷路了。

Public Sub FindAllOrphanFiles(ByVal targetDirectory As String)
    Dim fileEntries As String() = Directory.GetFiles(targetDirectory)

    ' Process the list of files found in the directory. 
    Dim files As String
    Dim iCount As Integer = 0
    Dim fileDetails As String(,)
    For Each files In fileEntries
        Dim fileIcon As String
        Dim thisFile As New IO.FileInfo(files)

        Dim fileName As String = thisFile.Name
        Dim fileSize As String = thisFile.Length
        Dim fileDateModified As String = thisFile.LastWriteTime
        Dim fileExtension As String = Path.GetExtension(fileName)
        Dim fileShortPath As String = Replace(Replace(files, uploadFolderPath, ""), fileName, "")
        Dim fileFullPath As String = files
        If fileExtension = ".pdf" Then
            fileIcon = "acrobat"
        Else
            fileIcon = "paint"
        End If

        ' Write to Array
        fileDetails(iCount, 0) = fileIcon
        fileDetails(iCount, 1) = fileName
        fileDetails(iCount, 2) = fileShortPath
        fileDetails(iCount, 3) = fileDateModified
        fileDetails(iCount, 4) = fileSize
        fileDetails(iCount, 5) = fileFullPath

        iCount += 1
    Next files
    Dim subdirectoryEntries As String() = Directory.GetDirectories(targetDirectory)

    ' Recurse into subdirectories of this directory. 
    Dim subdirectory As String
    For Each subdirectory In subdirectoryEntries
        FindAllOrphanFiles(subdirectory)
    Next subdirectory

End Sub 'FindAllOrphanFiles

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

您的阵列未初始化。如果您在循环之前知道某个点的大小,则应使用REDIM初始化它:

Dim fileDetails As String(,)
redim fileDetails(fileEntries.Count -1,5)
For Each files In fileEntries
  ....

如果您不提前知道,请在循环中使用Redim Preserve:

Dim fileDetails As String(,)
Dim I as int32 = -1
For Each files In fileEntries
    I += 1
    redim preserve fileDetails(i,5)
    ....