[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 152
  • Last Modified:

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?
0
mackeyrj
Asked:
mackeyrj
  • 3
1 Solution
 
crisco96Commented:
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

0
 
k_swapnilCommented:
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
0
 
mackeyrjAuthor Commented:
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

0
 
mackeyrjAuthor Commented:
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

0
 
mackeyrjAuthor Commented:
Found it. I was redimming my collection in the sub new.
Had to just use

Sub New()
        mParent = New Collection
    End Sub
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now