因此,一些用于填充数组的循环没有运行,我不知道为什么。我正在从包含以下数据的文本文件中读取数据。
Jazz
Jets
Owls
Rams
Cubs
Zips
此文件名为Names.txt 我正在读它并将其分配给一个数组。然后我将它传递给一个流行的数组结构,但循环确实运行,我不明白为什么。
Option Strict On
Public Class frmScoccer
Public Structure Team
Dim name As String
Dim wins As Integer
End Structure
Dim output As Team() 'this is going to be holder for the output data.
Private Sub frmScoccer_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'this is going to be doing everything that needs to be done at load time.
Dim grabNames() As String = IO.File.ReadAllLines("Names.txt") 'this is to pull the names from the text file.
Dim names() As String 'this is going to be the array that holds the names.
'this is going assigned the names to the array
Dim grabWins() As String = IO.File.ReadAllLines("Wins.txt") 'this is going to be used to grab the data on wins before it is assigned to the array.
Dim wins(,) As String 'this is going to hold wins and loses.
' output(2).name = "bacon" 'this is just for troubleshooting and does not serve a purpose.
nameAssignment(grabNames, output)
' winAssignment(grabWins, wins)
' countIf(wins, output) 'this is going to count the number of wins per team.
End Sub
Sub countIf(wins(,) As String, ByRef output As Team())
'this is going to bring the win counter up for a team if the value is true.
For row As Integer = 0 To (wins.GetUpperBound(0))
output(row).wins = 0
For column As Integer = 0 To (wins.GetUpperBound(1))
If wins(row, column) = "true" Then
output(row).wins += 1
End If
Next
Next
End Sub
Sub nameAssignment(grabNames() As String, ByRef output() As Team)
'this is goning to be a way to get the names from the input array to the useable array.
For Each team In grabNames
MessageBox.Show("loop is running")
output(CInt(team)).name = grabNames(CInt(team))
Next
End Sub
Sub winAssignment(grabWins() As String, ByRef wins(,) As Integer)
Dim line, data() As String
For row As Integer = 0 To (grabWins.GetUpperBound(0))
line = grabWins(row)
data = line.Split(","c)
For column As Integer = 0 To (grabWins.GetUpperBound(1))
wins(row, column) = CInt(data(column))
Next
Next
End Sub
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
'this is going to display the output
Dim query = From win In output
Order By win.wins Descending
Select win
For Each win In query
dgvTeamWins.DataSource = query.ToList
dgvTeamWins.CurrentCell = Nothing
dgvTeamWins.Columns("name").HeaderText = "Team"
dgvTeamWins.Columns("wins").HeaderText = "Wins"
Next
End Sub
Private Sub btnTesting_Click(sender As Object, e As EventArgs) Handles btnTesting.Click
'this is going to be for fast trouble shooting.
MessageBox.Show(output(2).name.ToString)
End Sub
End Class
它看起来应该可以工作,因为每个循环应该为每一行信息运行正确吗?我不明白每个循环如何工作,我没有正确格式化我的数据文件/读错了吗?
我知道它不起作用,因为当我运行我的测试寻找输出(1).name我得到一个空引用错误,我知道循环没有运行,因为循环中的消息框永远不会触发。
请原谅留言箱,它们是我用来试图找出错误发生的地方。
关于我的逻辑错误在哪里的任何想法?