Reputation: 170
When iterating over a text file as I do very frequently without issue the List(Of Franchise) is adding the last line in the the text file 12 times to my list rather than adding 12 unique items. Has anyone every experienced this before? I will use Linq and Lambda Expressions heavily later on once the Object is fully defined.
I tested my code to make sure I'm not crazy by populating Lists of Strings and verifying I am getting 12 unique values. It has something to do with my Object definition.
League Object
Public Class League
Public Property Owners As List(Of Francise)
Public Sub New()
Owners = New List(Of Francise)
End Sub
End Class
Franchise Object
Public Class Francise
Public Shared Property ownerName As String = ""
Public Shared Property ownerID As String = ""
Public Shared Property rosteredPlayers As List(Of Player)
Public Shared Property startedPlayers As List(Of Player)
Public Shared Property benchedPlayers As List(Of Player)
Public Shared Property starterPoints As Decimal = 0
Public Shared Property benchPoints As Decimal = 0
Public Sub New()
rosteredPlayers = New List(Of Player)
startedPlayers = New List(Of Player)
benchedPlayers = New List(Of Player)
End Sub
End Class
Player Object
Public Class Player
Public Property name As String = ""
Public Property year As String = ""
Public Property week As String = ""
Public Property FantraxID As String = ""
Public Property StatsIncID As String = ""
Public Property sportradarId As String = ""
Public Property rotowireId As String = ""
Public Property team As String = ""
Public Property position As String = ""
Public Property status As String = ""
End Class
Class where I am accessing and populating the League object
' Create League Object
thisLeague = New League() ' <- previously undefined Shared object in another class
'*******read through settings and add the teams*******'
Using sr As StreamReader = New StreamReader(dir & "\LeagueSettings.txt")
currentLine = sr.ReadLine
Do While (Not currentLine Is Nothing)
If currentLine.Contains("id:") Then
Dim f As Francise = New Francise
'name
f.ownerName = currentLine.Split(","c)(1).Split(":"c)(1)
' ID
f.ownerID = currentLine.Split(","c)(2).Split(":"c)(1)
thisLeague.Owners.Add(f)
End If
currentLine = sr.ReadLine
Loop
End Using
Iterating over Text File
dailpcnij67b2yqn,name:Marlon Bermudez (@Marlonb_21),id:dailpcnij67b2yqn
1bk8tc3gj67b2zew,name:Bob Lung (@bob_lung),id:1bk8tc3gj67b2zew
qqzscc6jj67b2ymc,name:Josh (@FantasyADHD),id:qqzscc6jj67b2ymc
730u1741j67b2yl1,name:Patrick Bergman (@Phrankie_D),id:730u1741j67b2yl1
gidjhqi0j67b2za8,name:John Di Bari (@dibari22),id:gidjhqi0j67b2za8
57u5kpdjj67b2z5s,name:Kyle Shumway (@ffpadawan),id:57u5kpdjj67b2z5s
ie2nod6wj67b2yoh,name:LAJJ (@lajjjj),id:ie2nod6wj67b2yoh
7nbcgudyj67b2zj9,name:Rob Waziak (@WazNFL),id:7nbcgudyj67b2zj9
cxshguvxj67b40ca,name:Tim Wagner (@X_fan12),id:cxshguvxj67b40ca
jkxb910ij67b2ytk,name:Kenneth Cashman (@RotoWear),id:jkxb910ij67b2ytk
n33hlz39j67b2z16,name:Matt Harmon (@MattHarmon_BYB),id:n33hlz39j67b2z16
7h6ivrntj67b2yx4,name:Thor (@Thoreosnmilk),id:7h6ivrntj67b2yx4
Upvotes: 0
Views: 50
Reputation: 170
Thanks @Plutonix @HansPassant I got a little overzealous with my Shared Blocks when I was copy and pasting old code. None of the properties should have been shared.
Corrected Franchise Class
Public Class Francise
Public Property ownerName As String = ""
Public Property ownerID As String = ""
Public Property rosteredPlayers As List(Of Player)
Public Property startedPlayers As List(Of Player)
Public Property benchedPlayers As List(Of Player)
Public Property starterPoints As Decimal = 0
Public Property benchPoints As Decimal = 0
Public Sub New()
rosteredPlayers = New List(Of Player)
startedPlayers = New List(Of Player)
benchedPlayers = New List(Of Player)
End Sub
End Class
https://msdn.microsoft.com/en-us/library/4hbsxy95(v=vs.90).aspx
Upvotes: 0
Reputation: 4185
Assuming other part of the code has null check (file not exist, ex.) and all variables are properly defined previously. Try do the while loop like this -
Dim sr As StreamReader = New StreamReader(dir & "\LeagueSettings.txt")
Do While sr.Peek() >= 0
currentLine = sr.ReadLine
If currentLine.Contains("id:") Then
Dim f As Francise = New Francise
'name
f.ownerName = currentLine.Split(","c)(1).Split(":"c)(1)
' ID
f.ownerID = currentLine.Split(","c)(2).Split(":"c)(1)
thisLeague.Owners.Add(f)
End If
Loop
Upvotes: 1