smile232
smile232

Reputation: 1

Visual basic error "Variable is used before...."

Please help me! I wrote the following program, there are a mistake "Variable "arr" is used before..." I tried to fix, but I couldn't. Please help me:) Thanks a lot!

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

Upvotes: 0

Views: 497

Answers (1)

SLaks
SLaks

Reputation: 887215

You need to initialize the arr variable.

Since you don't know how big the array needs to be, you should use a List(Of T) instead.

Upvotes: 1

Related Questions