|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Storing collection of one class inside anothercollection 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! 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! > 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? 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? > 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? Bryan wrote:
Show quoteHide quote > Here is part of my class TagType: Same error? <checks thread> Oh, a null reference error.> > 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? > Private _Props As List(Of Prop) This declares _Props as a variable that references a List(Of Prop), butit 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
updatecommand
to read a textfile regarding his format Design Question for Factory Pattern and Casting Types Floating Menu Bar Session variable as string (array) using MDIList in a MDI form code convertor in VB.NET Express Edition Getting the Parent firectory of a file Access MDI child properties generically vb.net 2005 bug in the ide (wipes out code) |
|||||||||||||||||||||||