Visual Basic错误“变量在......之前使用”

时间:2012-04-19 20:33:24

标签: vb.net

请帮帮我!我写了下面的程序,有一个错误“变量”arr“以前用过......”我试图修复,但我不能。请帮帮我:)非常感谢!

Imports System
Imports System.IO

Class Test
    Public Shared Sub Main()
        Try
            ' Create an instance of StreamReader to read from a file.
            Dim sr As StreamReader = New StreamReader("a.txt")
            Dim line As String
            Dim arr()() As Char
            Dim tem As Integer
            tem = 1
            ' Read and display the lines from the file until the end 
            ' of the file is reached.
            Do
                line = sr.ReadLine()
                arr(tem) = line.ToCharArray
                Console.WriteLine(Line)
            Loop Until line Is Nothing
            sr.Close()
        Catch E As Exception
            ' Let the user know what went wrong.
            Console.WriteLine("The file could not be read:")
            Console.WriteLine(E.Message)
        End Try
    End Sub
End Class

Module Module1
    Sub Main()
        Test.Main()
    End Sub
End Module

1 个答案:

答案 0 :(得分:1)

您需要初始化arr变量。

由于您不知道数组需要多大,您应该使用List(Of T)代替。