GetUpperBound()始终返回零

时间:2014-04-23 00:05:45

标签: vb.net

为什么intLength = dblFileArray.GetUpperBound(0)总是返回0?我注释掉了使用dblFileArray的程序的其余部分,并创建了一个文本框,显示dblFileArray的值以进行故障排除。 我知道我可以使用List,但我想使用数组。

Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
    ' stores the contents of the numbers.txt file in an array
    ' increases by 1 each number stored in the array
    ' writes the array contents to the file
    Dim dblFileArray(0) As Double
    Dim inFile As IO.StreamReader
    Dim outFile As IO.StreamWriter = Nothing
    Dim intLength As Integer
    lstNumbers.Items.Clear()

    inFile = IO.File.OpenText("Numbers.txt")
    Do Until inFile.Peek = -1
        Dim index As Integer = 0

        Double.TryParse(inFile.ReadLine, dblFileArray(index))
        dblFileArray(index) += 1
        lstNumbers.Items.Add(dblFileArray(index).ToString)
        ReDim Preserve dblFileArray(index)
        index += 1
    Loop

    inFile.Close()

    intLength = dblFileArray.GetUpperBound(0)
    txtBox.Text = intLength.ToString
    'outFile = IO.File.CreateText("Numbers.txt")
    ' For index As Integer = 0 To intLength
    '
    'outFile.WriteLine(dblFileArray(index))
    '   index += 1
    '  Next

    'outFile.Close()

End Sub

1 个答案:

答案 0 :(得分:3)

因为你在循环中声明索引= 0,所以值总是相同的。在循环开始之前声明它,并在while循环结束时递增它。 您应该只使用具有修复长度的数组。因为表现不佳,所以不要选择它。如果你需要一个动态数组去寻找列表,arraylist或其他什么。他们会避免你很多麻烦。