# Need some VB help (Array of Objects)!!



## r3wind89 (Apr 24, 2008)

[email protected]llo 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:


```
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:


```
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:


```
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:


```
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!


----------



## pvc_ (Feb 18, 2008)

I don't think you need the For loop in there, other than that I don't see any problems with the code. Are you getting any errors?
oh btw, I didn't know about redim "preserve", I guess that's a really nice feature.

To test your code, you can put another loop after you close the streamreader that would grab the objects from the array and converts them back to the player class and then prints the score and name variables.


----------



## pvc_ (Feb 18, 2008)

ok, now I see it, after getting rid of the For loop, you also have to change the following code:


```
pPlayers(Index) = New PlayerClass(Firstname, Score)
```
to


```
pPlayers(FieldArray.GetUpperBound(0)) = New PlayerClass(Firstname, Score)
```
if FieldArray.GetUpperBound(0) gives you error, you might have to change it to FieldArray.GetUpperBound(0) -1


----------



## r3wind89 (Apr 24, 2008)

Thanks for the reply pvc, i have done what you have said, removed the for loop and modified the line of code you said too. I now have the following:


```
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


        If (File.Exists("players.txt")) Then

            FileReader = File.OpenText("players.txt")

            While FileReader.Peek <> -1

                LineIn = FileReader.ReadLine()
                FieldArray = Split(LineIn, ",")
                For Index = 0 To FieldArray.GetUpperBound(0)
                    Firstname = FieldArray(Index)
                    Score = CInt(FieldArray(Index + 1))

                    pPlayers(FieldArray.GetUpperBound(0)) = New PlayerClass(Firstname, Score)

                    If LineIn <> "" Then
                        ReDim Preserve pPlayers(pPlayers.Length)
                    End If

                Next

            End While
            FileReader.Close()


            'display playernames in list box
            For Index = 0 To pPlayers.GetUpperBound(0)
                lstPlayers.Items.Add(CType(pPlayers(Index), PlayerClass).firstname)
            Next
        Else
            MessageBox.Show("The Players file you were looking for does not exist or is in the wrong place")
        End If


    End Sub
```
I am getting an exception error at runtime with the line of code:


```
pPlayers(FieldArray.GetUpperBound(0)) = New PlayerClass(Firstname, Score)
```
The error says: NullReferenceException was unhandled, "Object reference not set to an instance of an object."

I have tried adding the -1 at the upperbound but that still doesnt change the error!

Help!


----------



## pvc_ (Feb 18, 2008)

in the code you posted, you still have the for loop. Also, use this instead:


```
pPlayers(pPlayers.GetUpperBound(0)) = New PlayerClass(Firstname, Score)
```
you may have to use -1.


----------



## pvc_ (Feb 18, 2008)

```
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


        If (File.Exists("players.txt")=false) Then 
             MessageBox.Show("The Players file you were looking for does not exist or is in the wrong place")
             Exit sub
        end if

            FileReader = File.OpenText("players.txt")

            While FileReader.Peek <> -1

                LineIn = FileReader.ReadLine()
                FieldArray = Split(LineIn, ",")

                    Firstname = FieldArray(0
                    Score = CInt(FieldArray(1))

                    pPlayers(Players.GetUpperBound(0)-1) = New PlayerClass(Firstname, Score)

                    If LineIn <> "" Then
                        ReDim Preserve pPlayers(pPlayers.Length)
                    End If


            End While
            FileReader.Close()


            'display playernames in list box
            For Index = 0 To pPlayers.GetUpperBound(0)
                lstPlayers.Items.Add(CType(pPlayers(Index), PlayerClass).firstname)
            Next


    End Sub
```


----------



## gurutech (Apr 23, 2004)

Sounds like a programming course I recently took....

If I remember correctly, there's a "Dim" statement similar to "Dim (Name) As Array" that will allow you to define the array.


----------

