Link to home
Start Free TrialLog in
Avatar of mackeyrj
mackeyrj

asked on

Using a collection (of objects) in a class

I have a class calledPlayer and a class called Parent.
The player class has a field called mParents and a property called Parents.
mParents is a collection. This way each player can have multiple parents.

At this point I have a very simple addParent method in Player that looks like this.

    Public Sub addParent(ByVal value As Parent)
        Dim mParent = New Collection
        mParent.Add(value)
    End Sub


I use this method this way.

Private Sub addAParent(ByVal newPlayer As Player, ByVal nameOfParent As Parent)
        Dim aplayer As Player
        aplayer = New Player
        aplayer = newPlayer

        Dim aPorG As ParentorGuardian
        aPorG = New ParentorGuardian
        aPorG = nameOfParent
        aplayer.addParent(aPorG)


        TextBox2.Text = aplayer.Parent.Item(1)
    End Sub


I get the error.... Object reference not set to an instance of an object.

aPlayer has it's info, aPoG has it's info.
What am I doing wrong?
Avatar of crisco96
crisco96
Flag of United States of America image

Your'e setting aplayer and aPorG to the variables newPlayer and nameOfParent both of which I'm guessing are null.  If you want newPlayer and nameOfParent to be instances of aplayer and aPorG then you have to flip them around the equal sign.
Private Sub addAParent(ByVal newPlayer As Player, ByVal nameOfParent As Parent)
        Dim aplayer As Player
        aplayer = New Player
        newPlayer = aplayer

        Dim aPorG As ParentorGuardian
        aPorG = New ParentorGuardian
        nameOfParent = aPorG
        aplayer.addParent(aPorG)


        TextBox2.Text = aplayer.Parent.Item(1)
    End Sub

Open in new window

Hi,

You have declared mParent as local variable to function addParent. due to this, the property aplayer.Parent is always null and thats what causing the problem.

Make it a class level member. For eg:

Pubic Class Player
{
Dim mParent = New Collection

Public Collection Parent
{
get {return mParent; }
set {mParent = value; }
}
 
Public Sub addParent(ByVal value As Parent)
    mParent.Add(value)
End Sub
}

PS: sorry for the mixed code...I am not too familiar with VB code. Hope it solves the purpose.

Cheers!
Swapnl
Avatar of mackeyrj
mackeyrj

ASKER

Let me clean this up and show the step that comes first.

I have a form with two textboxes and a button. (Just for testing)
In the first textbox I enter what will be the parents first name
I press the button and the code will input the players first name and the parents last name.

(makes no sense... just for testing)


 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim newParent As Parent
        newParent = New Parent
        Dim newPlayer As Player
        newPlayer = New Player

        newPlayer.FirstName = "Jeff"

        newParent.FirstName = TextBox1.Text
        newParent.LastName = "LastName"
        writeName(newPlayer, newParent)

    End Sub

    Private Sub writeName(ByVal newPlayer As Player, ByVal nameOfParent As Parent)

newPlayer.AddParent(nameOfParent)

        TextBox2.Text = newPlayer.ParentorGaurdian.Item(1)
    End Sub

Open in new window

Here is my rewritten Player class which still returns the error

All but relevant code removed.

 Object reference not set to an instance of an object.
Public Class Player

    Public mParent As Collection

    Public Sub New()
        Dim mParent = New Collection
    End Sub

    Public Property Parent() As Collection
        Get
            Return mParent
        End Get
        Set(ByVal value As Collection)
            mParent = value
        End Set
    End Property

    Public Sub addParent(ByVal value As ParentorGuardian)
        mParent.Add(value)
    End Sub
End Class

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of mackeyrj
mackeyrj

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial