There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot bsod computer connection cpu crash css dell desktop dma driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware monitor motherboard network networking outlook outlook 2003 outlook 2007 outlook express pio problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
Need some VB help (Array of Objects)!!


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
r3wind89's Avatar
Junior Member with 2 posts.
 
Join Date: Apr 2008
Experience: Intermediate
24-Apr-2008, 05:21 AM #1
Need some VB help (Array of Objects)!!
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!
pvc_'s Avatar
Computer Specs
Member with 91 posts.
 
Join Date: Feb 2008
Location: Orlando, FL
Experience: Advanced
24-Apr-2008, 06:57 AM #2
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_'s Avatar
Computer Specs
Member with 91 posts.
 
Join Date: Feb 2008
Location: Orlando, FL
Experience: Advanced
24-Apr-2008, 07:03 AM #3
ok, now I see it, after getting rid of the For loop, you also have to change the following code:

Code:
pPlayers(Index) = New PlayerClass(Firstname, Score)
to

Code:
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's Avatar
Junior Member with 2 posts.
 
Join Date: Apr 2008
Experience: Intermediate
24-Apr-2008, 08:36 AM #4
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:

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


        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:

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!

Last edited by r3wind89 : 24-Apr-2008 09:35 AM.
pvc_'s Avatar
Computer Specs
Member with 91 posts.
 
Join Date: Feb 2008
Location: Orlando, FL
Experience: Advanced
26-Apr-2008, 12:31 AM #5
in the code you posted, you still have the for loop. Also, use this instead:

Code:
pPlayers(pPlayers.GetUpperBound(0)) = New PlayerClass(Firstname, Score)
you may have to use -1.
pvc_'s Avatar
Computer Specs
Member with 91 posts.
 
Join Date: Feb 2008
Location: Orlando, FL
Experience: Advanced
26-Apr-2008, 12:33 AM #6
This is what I mean
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


        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's Avatar
Computer Specs
Distinguished Member with 2,208 posts.
 
Join Date: Apr 2004
Location: Central NJ
Experience: Seasoned Professional
28-Apr-2008, 08:15 PM #7
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.
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 01:36 PM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.