当我经常迭代文本文件而没有问题时, List(Of Franchise)将文本文件中的最后一行添加到我的列表中12次而不是添加12个唯一项。以前有没有人经历过这个?一旦对象完全定义,我将在稍后使用Linq和Lambda表达式。
我测试了我的代码以确保通过填充字符串列表并验证我获得12个唯一值来确保我不会疯狂。它与我的对象定义有关。
联盟对象
Public Class League
Public Property Owners As List(Of Francise)
Public Sub New()
Owners = New List(Of Francise)
End Sub
End Class
特许经营对象
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
玩家对象
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
我正在访问和填充联盟对象的类
' 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
迭代文本文件
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
答案 0 :(得分:1)
假设代码的其他部分具有空检查(文件不存在,例如),并且之前已正确定义了所有变量。尝试像这样做while循环 -
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
答案 1 :(得分:0)
感谢@Plutonix @HansPassant 当我复制并粘贴旧代码时,我对共享块有点过分热心。这些属性都不应该被共享。
更正特许经营类
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