So some reason a loop that is meant to populate an array with is not running and I am not sure why. I am reading data from a text files that contains the following data.
Jazz
Jets
Owls
Rams
Cubs
Zips
this file is called Names.txt I am reading it in and assigning that to an array. I am then passing it to a sub to popular an array structure but the loop does run and I do not understand why.
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
It looks like it should work because a for each loop should run for each line of information correct? Am I not understanding how a for each loop works, am I not properly formatting my data file/reading it wrong?
I know it is not working because when I run my test looking for output(1).name I get a null reference error, I know the loop is not running because the messagebox in the loop never triggers.
Please forgive the messageboxes, they are what I use to try to find out where errors are happening.
Any ideas on where my logic is wrong?