Visual Basic列表添加时更新其他元素

时间:2015-01-25 14:47:19

标签: vb.net list

我在Visual Basic中有一个列表来添加运动员。如果列表为空,则项目正确添加。但是,如果我想添加另一个项目,但它会用新添加的项目替换其他项目。

从Add Athlete表格中我称之为:

Club.AddAthlete(New Athlete(TextBoxName.Text, TextBoxEmail.Text,
        TextBoxNumber.Text, DateTimePicker.Value.ToShortDateString()))

然后在班级俱乐部,我有一个由运动员组成的名单:

Public Class Club
    Public Shared athletes As New List(Of Athlete)()

    Public Shared Sub AddAthlete(ByVal r As Athlete)
        athletes.Add(r)
    End Sub

例如,我使用信息(“name1”,“email1”,“number1”,25/01/15)调用Add,这会正确添加,因为它是第一个项目

然后我再次使用此信息(“name2”,“email2”,“number2”,24/01/15)再次调用Add,这会添加,但会将第一项的数据更改为此数据

如果有人可以帮忙解决这个问题,我将非常感激,


Public Class Athlete 
    Shared Name As String = "" 
    Shared Email As String = "" 
    Shared Number As String = "" 
    Shared DoB As String = "" 
    Sub New(InName As String, InEmail As String, 
              InNumber As String, InDoB As String) 
        Name = InName 
        Email = InEmail 
        Number = InNumber 
        DoB = InDoB 
    End Sub

1 个答案:

答案 0 :(得分:1)

这些函数中只有少数应该有一个共享修饰符,其余的应该使用不同类的每个实例的数据,并且不应该是共享。您还可以通过将GetXXXX和SetXXXX函数替换为属性来简化代码(如果您不想允许Set,请将其设置为ReadOnly属性)。这是一个例子。

Imports System.IO
Public Class Athlete
    Public Property Name As String
    Public Property Email As String
    Public Property Number As String
    Public Property DoB As String

    Sub New(InName As String, InEmail As String, InNumber As String, InDoB As String)
        Name = InName
        Email = InEmail
        Number = InNumber
        DoB = InDoB
    End Sub

    Public Overrides Function ToString() As String
        Return Name
    End Function

    Public Sub Save(textOut As TextWriter)
        textOut.WriteLine(Name)
        textOut.WriteLine(Email)
        textOut.WriteLine(Number)
        textOut.WriteLine(DoB)
    End Sub

    Public Shared Function Load(textIn As TextReader) As Athlete
        Dim name As String = textIn.ReadLine()
        Dim email As String = textIn.ReadLine()
        Dim number As String = textIn.ReadLine()
        Dim dob As String = textIn.ReadLine()
        Return New Athlete(name, email, number, dob)
    End Function
End Class

Public Class Club
    Public Property ClubName As String
    Public Property Athletes As New List(Of Athlete)()

    Public Sub New(inClubName As String)
        ClubName = inClubName
    End Sub

    Public Sub SaveText(textOut As TextWriter)
        textOut.WriteLine(ClubName)
        textOut.WriteLine(Athletes.Count)
        For Each ath As Athlete In Athletes
             ath.Save(textOut)
        Next
    End Sub

    Public Sub Save()
        Using textOut As New StreamWriter("runnersData.txt")
            SaveText(textOut)
        End Using
    End Sub

    Public Shared Function Load(textIn As TextReader) As Club
        Dim myClub As New Club(textIn.ReadLine())
        Dim noOfRunners As Integer
        Integer.TryParse(textIn.ReadLine(), noOfRunners)
        For i As Integer = 0 To noOfRunners - 1
            myClub.Athletes.Add(Athlete.Load(textIn))
        Next
    Return myClub
    End Function

    Public Shared Function Load() As Club
        Return Load(New StreamReader("runnersData.txt"))
    End Function
End Class