The specific issue here is that you specify a file number of 1 when you open the file:
FileOpen(1, "example1.txt", OpenMode.Input)
but then you use different file numbers when you read the lines:
name(i) = LineInput(i)
You're not telling LineInput
to read a specific line. You're telling it to read the next line from a specific file. That should be:
name(i) = LineInput(1)
This is an example of why you should not use magic numbers in your code. It's too easy to mess them up. If you want to use the same value for the same reason in multiple places, assign it to a variable or even a constant and then use that each time:
Dim fileNumber = 1
FileOpen(fileNumber, "example1.txt", OpenMode.Input)
While Not EOF(fileNumber)
name(i) = LineInput(fileNumber)
EDIT:
To achieve equivalent functionality with good VB.NET code, you would do something like this:
Imports System.IO
Module Module1
Sub Main()
Dim names As New List(Of String)
Using inputFile As New StreamReader("example1.txt")
While Not inputFile.EndOfStream
Dim name = inputFile.ReadLine()
names.Add(name)
Console.WriteLine(name)
End While
End Using
Console.ReadLine()
End Sub
End Module
or this:
Imports System.IO
Module Module1
Sub Main()
Dim names As New List(Of String)
For Each name In File.ReadLines("example1.txt")
names.Add(name)
Console.WriteLine(name)
Next
Console.ReadLine()
End Sub
End Module
or this:
Imports System.IO
Module Module1
Sub Main()
Dim names = File.ReadAllLines("example1.txt")
For Each name In names
Console.WriteLine(name)
Next
Console.ReadLine()
End Sub
End Module
Note that the last example is slightly different because names
is an array rather than a collection and the entire file is read first, before each name is output to the console. It would appear the same from the user's perspective though. Once they are populated, you can pretty much use an array and a collection in exactly the same way.