执行了布尔数组,但索引在数组区域之外
An Array of Boolean executed, but the index is outside of the array region
这里遇到了一个问题,实在是解决不了。我已经创建了一个布尔数组,但是当我在 For Each Next 循环中执行我的命令时,它说索引超出了数组范围。下面我将简要介绍一下我的代码。
'This subroutine reads each line of the .txt file, each line contains a String
Sub openFile (ByRef list1() As String, ByVal FileName As String)
Dim i As Integer
i = 0
' Open file.txt with the Using statement.
Using r As StreamReader = New StreamReader(FileName)
Try
' Store contents in this String.
Dim line As String
' Read first line.
line = r.ReadLine()
' Loop over each line in file, While list is Not Nothing.
Do While (Not line Is Nothing)
' Add this line to list.
list1(i) = line
line = r.ReadLine()
i = i + 1
Loop
Catch E As System.Exception
MessageBox.Show(E.Message)
End Try
End Using
End Sub
Sub Main()
'The list contains only 60 lines
Dim list(59) As String
Dim myDocs As String
myDocs = "C:\Location I have saved on my desktop"
Dim inputFile As String = IO.Path.Combine(myDocs, "names_of_sketches.txt")
openFile(list, inputFile)
'The Strings written in the .txt file is exactly the same as this list of string
Dim profile_names As New System.Collections.Generic.List(Of String) From
{
1_2
1_3
.
.
.
15_16
}
Dim A() As Boolean = {False, False, False, ..., False} '60 Falses are written here
'What this for loop does is : if the .txt file contains the name in the list of strings, then make the array of Boolean True, if not then that Boolean element stays False
For Each f As String In list
Dim index As Integer = profile_names.IndexOf(f)
A(index) = True
Next
For example, let's say the contents in the .txt file is like this :
1_2
1_3
1_5
1_7
.
.
.
Then the array of Boolean should become :
{True, True, False, True, False, True...}
我不知道这里到底是什么问题,我尝试像下面这样设置布尔数组:
但是电脑说这是一个无效的表达式
请在这方面寻求帮助
问候
删除了硬编码 'magic number' (60) 并进行了一些其他更改。看看有没有帮助。
'This subroutine reads each line of the .txt file, each line contains a String
Sub openFile(AList As System.Collections.Generic.List(Of String),
FileName As String)
' Open file.txt with the Using statement.
Using r As StreamReader = New StreamReader(FileName)
Try
' Store contents in this String.
Dim line As String
' Read first line.
line = r.ReadLine()
' Loop over each line in file, While line is Not Nothing.
Do While line IsNot Nothing
' Add this line to list.
AList.Add(line)
line = r.ReadLine()
Loop
Catch E As System.Exception
MessageBox.Show(E.Message)
End Try
End Using
End Sub
Sub Main()
Dim SomeList As New System.Collections.Generic.List(Of String)
Dim myDocs As String
myDocs = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
Dim inputFile As String = IO.Path.Combine(myDocs, "test.txt")
openFile(SomeList, inputFile)
'initialize Booleans
Dim A As System.Collections.Generic.List(Of Boolean)
A = (From l In SomeList Select False).ToList
'The Strings written in the .txt file is exactly the same as this list of string
Dim profile_names As New System.Collections.Generic.List(Of String)
'What this for loop does is : if the .txt file contains the name in the list of strings, then make the array of Boolean True, if not then that Boolean element stays False
For Each f As String In SomeList
Dim index As Integer = profile_names.IndexOf(f)
If index >= 0 Then
A(index) = True
End If
Next
End Sub
您的 openFile
方法应该是一个函数,而不是将 list1
作为 ByRef 变量传递。不仅如此,还可以大大简化。看看这个例子:
Private Function ReadFileLines(filename As String) As String()
If (String.IsNullOrWhitespace(filename)) Then
Throw New ArgumentNullException(NameOf(filename))
End If
If (Not IO.File.Exists(filename)) Then
Throw New ArgumentException($"The file does not exist at: {filename}")
End If
Return IO.File.ReadAllLines(filename)
End Function
然后要使用它,您可以这样做:
Dim myDocs = "C:\Location I have saved on my desktop"
Dim list = ReadFileLines(myDocs)
我怀疑发生的事情是您已将 list1
声明为 60 项的固定长度,但当您的文件包含超过 60 行时。
这里遇到了一个问题,实在是解决不了。我已经创建了一个布尔数组,但是当我在 For Each Next 循环中执行我的命令时,它说索引超出了数组范围。下面我将简要介绍一下我的代码。
'This subroutine reads each line of the .txt file, each line contains a String
Sub openFile (ByRef list1() As String, ByVal FileName As String)
Dim i As Integer
i = 0
' Open file.txt with the Using statement.
Using r As StreamReader = New StreamReader(FileName)
Try
' Store contents in this String.
Dim line As String
' Read first line.
line = r.ReadLine()
' Loop over each line in file, While list is Not Nothing.
Do While (Not line Is Nothing)
' Add this line to list.
list1(i) = line
line = r.ReadLine()
i = i + 1
Loop
Catch E As System.Exception
MessageBox.Show(E.Message)
End Try
End Using
End Sub
Sub Main()
'The list contains only 60 lines
Dim list(59) As String
Dim myDocs As String
myDocs = "C:\Location I have saved on my desktop"
Dim inputFile As String = IO.Path.Combine(myDocs, "names_of_sketches.txt")
openFile(list, inputFile)
'The Strings written in the .txt file is exactly the same as this list of string
Dim profile_names As New System.Collections.Generic.List(Of String) From
{
1_2
1_3
.
.
.
15_16
}
Dim A() As Boolean = {False, False, False, ..., False} '60 Falses are written here
'What this for loop does is : if the .txt file contains the name in the list of strings, then make the array of Boolean True, if not then that Boolean element stays False
For Each f As String In list
Dim index As Integer = profile_names.IndexOf(f)
A(index) = True
Next
For example, let's say the contents in the .txt file is like this :
1_2
1_3
1_5
1_7
.
.
.
Then the array of Boolean should become :
{True, True, False, True, False, True...}
我不知道这里到底是什么问题,我尝试像下面这样设置布尔数组:
但是电脑说这是一个无效的表达式
请在这方面寻求帮助 问候
删除了硬编码 'magic number' (60) 并进行了一些其他更改。看看有没有帮助。
'This subroutine reads each line of the .txt file, each line contains a String
Sub openFile(AList As System.Collections.Generic.List(Of String),
FileName As String)
' Open file.txt with the Using statement.
Using r As StreamReader = New StreamReader(FileName)
Try
' Store contents in this String.
Dim line As String
' Read first line.
line = r.ReadLine()
' Loop over each line in file, While line is Not Nothing.
Do While line IsNot Nothing
' Add this line to list.
AList.Add(line)
line = r.ReadLine()
Loop
Catch E As System.Exception
MessageBox.Show(E.Message)
End Try
End Using
End Sub
Sub Main()
Dim SomeList As New System.Collections.Generic.List(Of String)
Dim myDocs As String
myDocs = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
Dim inputFile As String = IO.Path.Combine(myDocs, "test.txt")
openFile(SomeList, inputFile)
'initialize Booleans
Dim A As System.Collections.Generic.List(Of Boolean)
A = (From l In SomeList Select False).ToList
'The Strings written in the .txt file is exactly the same as this list of string
Dim profile_names As New System.Collections.Generic.List(Of String)
'What this for loop does is : if the .txt file contains the name in the list of strings, then make the array of Boolean True, if not then that Boolean element stays False
For Each f As String In SomeList
Dim index As Integer = profile_names.IndexOf(f)
If index >= 0 Then
A(index) = True
End If
Next
End Sub
您的 openFile
方法应该是一个函数,而不是将 list1
作为 ByRef 变量传递。不仅如此,还可以大大简化。看看这个例子:
Private Function ReadFileLines(filename As String) As String()
If (String.IsNullOrWhitespace(filename)) Then
Throw New ArgumentNullException(NameOf(filename))
End If
If (Not IO.File.Exists(filename)) Then
Throw New ArgumentException($"The file does not exist at: {filename}")
End If
Return IO.File.ReadAllLines(filename)
End Function
然后要使用它,您可以这样做:
Dim myDocs = "C:\Location I have saved on my desktop"
Dim list = ReadFileLines(myDocs)
我怀疑发生的事情是您已将 list1
声明为 60 项的固定长度,但当您的文件包含超过 60 行时。