Question List inside List

aaresmi

New member
Joined
Oct 12, 2013
Messages
1
Programming Experience
3-5
I need that one property of a list will be other list, is posible??

I am trying something like:
Public Class List1
        Private _aaaa As String
        Private _bbbb As String
        Private _dddd As List2
        Public Property aaaa() As String
            Get
                Return _aaaa
            End Get
            Set(ByVal value As String)
                _aaaa = value
            End Set
        End Property
        Public Property bbbb() As String
            Get
                Return _bbbb
            End Get
            Set(ByVal value As String)
                _bbbb = value
            End Set
        End Property
        Public Property dddd As List2
            Get
                Return _dddd
            End Get
            Set(ByVal value As List2)
                _dddd = value
            End Set
        End Property
End Class
 
Public Class List2
        Private _eeee As Integer
        Private _ffff As String
        Public Property _eeee As Integer
            Get
                Return _eeee
            End Get
            Set(ByVal value As Integer)
                _eeee = value
            End Set
        End Property
        Public Property ffff As String
            Get
                Return _ffff
            End Get
            Set(ByVal value As String)
                _ffff = value
            End Set
        End Property
End Class
 


Public Class List1
        Inherits List(Of List1)
End Class
Public Class List2
        Inherits List(Of List2)
End Class

But when try to charge the List2 i get one exception "null value" because List2 is not initialitated

Dim Lst As Lista1
For I = 0 To 6
 
     For X = 0 To 5
          Lista1(I).List2(X).eeee = 1
          Lista1(I).List2(X).ffff = 1
     Next
 
Next

Anyone can help me? What am i doing wrong?
 
Your code makes no sense. How can List1 inherit List(Of List1)? That would make it a list of itself. You need four classes; not two.
Public Class Item1

    Public Property Name1 As String

End Class

Public Class List1
    Inherits Collection(Of Item1)
End Class

Public Class Item2

    Private ReadOnly _items1 As New List1

    Public Property Name2 As String

    Public ReadOnly Property Items1 As List1
        Get
            Return _items1
        End Get
    End Property

End Class

Public Class List2
    Inherits Collection(Of Item2)
End Class
Notice the more correct inheritence of the System.Collections.ObjectModel.Collection(Of T) class rather than List(Of T). You can then write code like this:
Dim items2 As New List2

items2.Add(New Item2 With {.Name2 = "First"})
items2.Add(New Item2 With {.Name2 = "Second"})
items2.Add(New Item2 With {.Name2 = "Third"})

For Each item2 As Item2 In items2
    item2.Items1.Add(New Item1 With {.Name1 = item2.Name2 & ".First"})
    item2.Items1.Add(New Item1 With {.Name1 = item2.Name2 & ".Second"})
    item2.Items1.Add(New Item1 With {.Name1 = item2.Name2 & ".Third"})
Next

For Each item2 As Item2 In items2
    For Each item1 As Item1 In item2.Items1
        MessageBox.Show(item1.Name1, "First Run")
    Next
Next

items2(1).Items1(1).Name1 &= ".New"
items2(2).Items1(0).Name1 &= ".New"

For Each item2 As Item2 In items2
    For Each item1 As Item1 In item2.Items1
        MessageBox.Show(item1.Name1, "Second Run")
    Next
Next
 
I should also point out that you don't even need to define collection classes of your own. You can simply use List(Of Item1) and List(Of Item2). If you do want a custom collection though, inherit Collection(Of T).
 
Back
Top