列表内列表。那可能吗? [英] List inside List. Is that possible?

查看:75
本文介绍了列表内列表。那可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个列表中的一个属性将是其他列表,是否可用?



我正在尝试类似:



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





但是当我尝试对List2充电时,我得到一个例外空值,因为List2未初始化





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

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?

推荐答案

你必须实例化 List2 。声明它不会实例化它。请参阅下面的公共类List1 中的 Private _dddd As List2 = New List2



不要忘记你还必须实例化 List1



在嵌套的For..Next循环中,您将对类进行处理,就像它们是已使用一定数量的数组单元格声明的数组一样。您的课程中没有任何地方可以分配单元格来保存您在For..Next循环中指定的值。



请参阅Microsoft文档中的列表< t>类 [ ^ ]关于List类必须支持的一些功能的想法。



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.

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


非常感谢您的回答Mike Meinz。在主程序中,我实例化list1



Thank you very much for your answer Mike Meinz. In the main program I instantiate the list1

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





但现在错误是ArgumentoutofrangeException。错误在Lista1(I)中。 List2(X) .eeee = 1



你知道为什么吗?谢谢!!



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

Do you know why? Thx again!!


这篇关于列表内列表。那可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆