Reputation: 1
I have a list in Visual Basic to add an athlete. If the list is empty, the item adds correctly. However if I want to add another item, it does but it replaces the others items with the newly added item.
From a Add Athlete form I call this:
Club.AddAthlete(New Athlete(TextBoxName.Text, TextBoxEmail.Text,
TextBoxNumber.Text, DateTimePicker.Value.ToShortDateString()))
Then in the class Club, I have a list made up of Athletes:
Public Class Club
Public Shared athletes As New List(Of Athlete)()
Public Shared Sub AddAthlete(ByVal r As Athlete)
athletes.Add(r)
End Sub
For example I call Add with the information ("name1", "email1", "number1", 25/01/15) and this adds correctly as it is the first item
Then I call Add again with this information ("name2", "email2", "number2", 24/01/15) and this adds but it changes the data of the first item to this data
If anyone could help with this, I would be very grateful,
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
Upvotes: 0
Views: 231
Reputation: 4534
Only a few of those functions should have a Shared modifier, the rest are supposed to use data that is different for each instance of the class, and should not be Shared. You can also simplify your code by replacing the GetXXXX and SetXXXX functions with a property (if you don't want to allow Set, make it a ReadOnly Property). Here is an example.
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
Upvotes: 1