r3wind@hotmail.co.ukHello all,
I have been given the task of reading from a text file in the current format:
Name, Score
Name, Score
Name, Score
.... and so forth.
After reading the file i am required to enter the seperate values of name and score into an array, however the array must be an array of objects!
I have 2 data classes, one named PlayerClass and one named PersonClass both of which will be used to 'get' the players' name and scores later in the program.
PersonClass has a constructor as follows:
Code:
Public Sub New(ByVal pFirstname As String)
firstname = pFirstname
End Sub
firstname is an attribute declared at the top of the data class.
My other data class (PlayerClass) inherits PersonClass, however the constructor includes one extra parameter (score) as follows:
Code:
Public Sub New(ByVal pFirstname As String, ByVal pScore As Integer)
'pass parameters onto base class constructor
MyBase.New(pFirstname)
score = pScore
End Sub Again score is an attribute declared at the top of the data class.
I was given a skeleton form to work on which contained the following sub method:
Code:
Private Sub LoadPlayers(ByRef pPlayers() As PlayerClass)
'reads details of the player names and current scores from a delimited text file, into an array of objects
End Sub Now the thing im not too clear on is the parameter which has been entered into this sub, (ByRef pPlayers() as PlayerClass), so far i have managed to come up with the following code to read from the textfile seperated by comma delimeters:
Code:
Private Sub LoadPlayers(ByRef pPlayers() As PlayerClass)
'reads details of the player names and current scores from a delimited text file, into an array of objects
Dim FileReader As StreamReader
Dim LineIn As String
Dim FieldArray() As String
Dim Index As Integer
Dim Firstname As String
Dim Score As Integer
FileReader = File.OpenText("players.txt")
While FileReader.Peek <> -1
LineIn = FileReader.ReadLine()
FieldArray = Split(LineIn, ",")
For Index = 0 To FieldArray.GetUpperBound(0)
Firstname = FieldArray(0)
Score = CInt(FieldArray(1))
pPlayers(Index) = New PlayerClass(Firstname, Score)
If LineIn <> "" Then
ReDim Preserve pPlayers(pPlayers.Length)
End If
Next
End While
FileReader.Close()
End Sub I am aware that this code is mostly wrong but i had the best bash i could at it! I am just clueless on which step to take next, i know its a big ask but any help would be greatly appreciated, cheers guys!
