Home All Groups Group Topic Archive Search About

Storing collection of one class inside another

Author
14 May 2006 6:06 AM
Bryan
I have a class 'TagType' with an ilist member 'Props' that holds a
collection of another class called 'Prop'.  I let the user create
TagTypes and save multiple properties (Props) in them.
I am having a problem storing a Prop object in a TagType object.  The
code below is giving me a  "Object reference not set to an instance of
an object." error.  I don't understand why

        Dim t As TagType = Me.lstTagTypes.SelectedItem 'user selects
TagType from list
        Dim p As New Prop 'create new prop, assign it fake data
        With p
            .Name = "Phone #"
            .DataType = "String"
            .Value = "555-5555"
        End With
        t.Props.Add(p) 'add prop to Props collection (ilist) of
selected TagType  !!error!!

I am new to OOP, any help would be appreciated!

Author
14 May 2006 12:44 PM
Göran_Andersson
Does the TagType that you get from the selected item contain a list?
Have you created the actual list, or only declared a reference that can
be used to reference a list?

Bryan wrote:
Show quoteHide quote
> I have a class 'TagType' with an ilist member 'Props' that holds a
> collection of another class called 'Prop'.  I let the user create
> TagTypes and save multiple properties (Props) in them.
> I am having a problem storing a Prop object in a TagType object.  The
> code below is giving me a  "Object reference not set to an instance of
> an object." error.  I don't understand why
>
>         Dim t As TagType = Me.lstTagTypes.SelectedItem 'user selects
> TagType from list
>         Dim p As New Prop 'create new prop, assign it fake data
>         With p
>             .Name = "Phone #"
>             .DataType = "String"
>             .Value = "555-5555"
>         End With
>         t.Props.Add(p) 'add prop to Props collection (ilist) of
> selected TagType  !!error!!
>
> I am new to OOP, any help would be appreciated!
>
Author
14 May 2006 6:13 PM
Bryan
The class contains a list, so when I reference TagType shouldn't the
list be accesable?  Do I have to somehow initiate the list or dim it or
something?  Why is the list member of the class different than say a
regular string property?  Do I have to treat it differently?  Someone
else asked if I had an instance of the list, this kind of seems like
what you are asking.  Do I need to create an instance of the list
before I use it? If so, how?
Author
14 May 2006 7:25 PM
Göran_Andersson
Yes, you have to create an instance of the list before you use it. You
can do that in the constructor of the class:

Props = New WhatEverTheTypeOfTheList()

Whenever you declare a variable where the type is a class, you are only
declaring a reference. You also have to create an instance of the class
using the New keyword.

Strings are a bit special, though. Say that you declare a string and
give it a value:

Dim s as String
s = "test"

Here the variable s is just a reference, just as any variable where the
type is a class. What's special is that the string literal "test" is
actually an instance of the String class.

Bryan wrote:
Show quoteHide quote
> The class contains a list, so when I reference TagType shouldn't the
> list be accesable?  Do I have to somehow initiate the list or dim it or
> something?  Why is the list member of the class different than say a
> regular string property?  Do I have to treat it differently?  Someone
> else asked if I had an instance of the list, this kind of seems like
> what you are asking.  Do I need to create an instance of the list
> before I use it? If so, how?
>
Author
15 May 2006 3:12 AM
Bryan
Here is part of my class TagType:

Public Class TagType
    Public Sub New()
        _Name = ""
        _Description = ""
        Props = New List(Of Prop)
    End Sub
Private _Props As List(Of Prop)
    Public Property Props() As List(Of Prop)
        Get
            Return _Props
        End Get
        Set(ByVal value As List(Of Prop))
            _Props = value
        End Set
    End Property
end class

Then I try to store a couple of Prop objects in the collection like
this

        Dim t As TagType = Me.lstTagTypes.SelectedItem
        Dim p As New Prop
        With p
            .Name = Me.txtPropName.Text
            .DataType = Me.txtPropDataType.Text
            .Value = Me.txtPropValue.Text
        End With
        t.Props.Add(p)
        Next

But am still getting the same error.  What am I missing?
Author
15 May 2006 9:02 AM
Larry Lard
Bryan wrote:
Show quoteHide quote
> Here is part of my class TagType:
>
> Public Class TagType
>     Public Sub New()
>         _Name = ""
>         _Description = ""
>         Props = New List(Of Prop)
>     End Sub
> Private _Props As List(Of Prop)
>     Public Property Props() As List(Of Prop)
>         Get
>             Return _Props
>         End Get
>         Set(ByVal value As List(Of Prop))
>             _Props = value
>         End Set
>     End Property
> end class
>
> Then I try to store a couple of Prop objects in the collection like
> this
>
>         Dim t As TagType = Me.lstTagTypes.SelectedItem
>         Dim p As New Prop
>         With p
>             .Name = Me.txtPropName.Text
>             .DataType = Me.txtPropDataType.Text
>             .Value = Me.txtPropValue.Text
>         End With
>         t.Props.Add(p)
>         Next
>
> But am still getting the same error.  What am I missing?

Same error? <checks thread> Oh, a null reference error.

> Private _Props As List(Of Prop)

This declares _Props as a variable that references a List(Of Prop), but
it doesn't actually create a List(Of Prop) for it to reference.
Simplest fix:

Private _Props As New List(Of Prop)

Alternatively in the constructor

_Props = New List(Of Prop)

Much the same effect; some people like to keep all object creation code
in the constructor.

--
Larry Lard
Replies to group please