Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need that one property of a list will be other list, is posible??

I am trying something like:

VB
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

VB
For I = 0 To 6

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

Next


What am i doing wrong?
Posted
Updated 12-Oct-13 7:59am
v2

You have to instantiate List2. Declaring it does not instantiate it. See Private _dddd As List2 = New List2 in the Public Class List1 below.

Don't forget that you will have to instantiate List1, also.

In your nested For..Next loops, you are treating your classes as though they are arrays that have been declared with a set number of array cells. Nowhere in your classes do you allocate cells to hold the values that you are assigning in your For..Next loop.

See the Microsoft documentation for List <t> Class[^] for ideas on some of the "features" that your List classes must support.

VB.NET
Public Class List1
        Private _aaaa As String
        Private _bbbb As String
        Private _dddd As List2 = New 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
 
Share this answer
 
v5
Thank you very much for your answer Mike Meinz. In the main program I instantiate the list1

VB
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 = "pP"
     Next
Next


but now the error is ArgumentoutofrangeException. The error is in Lista1(I).List2(X).eeee =1

Do you know why? Thx again!!
 
Share this answer
 
Comments
Mike Meinz 12-Oct-13 16:08pm    
Please do not post followups as a separate Solution. Use "Have a Question or Comment" link.
Mike Meinz 12-Oct-13 16:15pm    
See revised Solution 1.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900