1

I am trying to run an event which will search through the different files in a given directory. The goal is to have it search for all files that begin with 'SP_', which are .sql files containing Stored Procedures. I would then like to add the full text of these Procedures to an array to be used later. This is causing an error when run, which I believe is because 'FullProcedureArray()', the string array I am trying to load does not have defined boundaries. When I declare it as 'FullProcedureArray(7)', or with some other value, it appears to run fine. But I don't want to have to hard-code a boundary for 'FullProcedureArray'; I would rather let it be defined by whatever the number of files in the folder is.

My question: Is there a way to declare 'FullProcedureArray' without having to give it an absolute value? I may just be missing something painfully obvious, but I haven't worked with this type of array much in the past. Thanks in advance for your help.

    Dim AppDataLocation As String = "C:\Files\TestFiles\"
    Dim ProcedureArray As String()
    Dim ProcedureText As String
    Dim FullProcedureArray() As String

    Dim sourceDirectoryInfo As New System.IO.DirectoryInfo(AppDataLocation)

    Dim fileSystemInfo As System.IO.FileSystemInfo
    Dim i As Integer = 0
    For Each fileSystemInfo In sourceDirectoryInfo.GetFileSystemInfos
        If (fileSystemInfo.Name.Contains("SP_")) Then
            ProcedureArray = System.IO.File.ReadAllLines(AppDataLocation & fileSystemInfo.Name)
            ProcedureText = Join(ProcedureArray, "")
            FullProcedureArray.SetValue(ProcedureText, i)
            i = (i + 1)
        End If
    Next

2 Answers 2

8

An array by definition has a fixed upper bound. If you don't want a fixed upper bound, don't use an array. Use, for example, a List(Of String) instead:

Dim AppDataLocation As String = "C:\Files\TestFiles\" 
Dim ProcedureList As New List(Of String)

Dim sourceDirectoryInfo As New System.IO.DirectoryInfo(AppDataLocation) 

For Each fileSystemInfo As System.IO.FileSystemInfo In sourceDirectoryInfo.GetFileSystemInfos 
    If (fileSystemInfo.Name.Contains("SP_")) Then 
        Dim ProcedureText As String = _
            System.IO.File.ReadAllText(AppDataLocation & fileSystemInfo.Name) 
        ProcedureList.Add(ProcedureText)
    End If 
Next 

If, for some reason, you still need the result as an array afterwards, simply convert the list to an array:

Dim myArray() As String = ProcedureList.ToArray()
Sign up to request clarification or add additional context in comments.

Comments

0

If you don't want to give a size to your array or want to change at runtime, you can use "Redim Preserve" http://msdn.microsoft.com/en-us/library/w8k3cys2%28v=vs.71%29.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.