vb.net 向数组添加值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9432937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:57:57  来源:igfitidea点击:

Adding values to array

vb.net

提问by A. Still

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.

我正在尝试运行一个事件,该事件将搜索给定目录中的不同文件。目标是让它搜索所有以“SP_”开头的文件,这些文件是包含存储过程的 .sql 文件。然后我想将这些程序的全文添加到一个数组中以供以后使用。这会在运行时导致错误,我认为这是因为“FullProcedureArray()”,我尝试加载的字符串数组没有定义的边界。当我将其声明为“FullProcedureArray(7)”或其他值时,它似乎运行良好。但我不想硬编码“FullProcedureArray”的边界;我宁愿让它由文件夹中的文件数量来定义。

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.

我的问题:有没有办法声明 'FullProcedureArray' 而不必给它一个绝对值?我可能只是遗漏了一些非常明显的东西,但我过去很少使用这种类型的数组。在此先感谢您的帮助。

    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

回答by Heinzi

An array by definitionhas 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:

根据定义,数组具有固定的上限。如果您不想要固定的上限,请不要使用数组。例如,使用 aList(Of String)代替:

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 convertthe list to an array:

如果由于某种原因,之后您仍然需要将结果作为数组,只需列表转换为数组:

Dim myArray() As String = ProcedureList.ToArray()

回答by Onur

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

如果您不想为数组指定大小或希望在运行时更改,可以使用“Redim Preserve” http://msdn.microsoft.com/en-us/library/w8k3cys2%28v=vs.71 %29.aspx