2012-04-19 71 views
0

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

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 
+0

之前干什么用的?你明白错误的含义吗? – SLaks 2012-04-19 20:34:56

+2

请注意,你刚刚(很差)重新发明'File.ReadAllLines()'。 – SLaks 2012-04-19 20:37:21

+0

警告\t \t 1变量“改编”它已经被分配了值之前被使用。运行时可能会导致空引用异常。而且我明白,但我无法找到修复它的方法 – smile232 2012-04-19 20:37:50

回答

1

您需要初始化arr变量。

既然你不知道数组需要有多大是,你应该使用List(Of T)代替。

+0

谢谢:)我知道了:) – smile232 2012-04-19 21:17:12