Storing collection of one class inside another

B

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

=?ISO-8859-1?Q?G=F6ran_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?
 
B

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

=?ISO-8859-1?Q?G=F6ran_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.
 
B

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?
 
L

Larry Lard

Bryan said:
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? said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top