Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 类中的VB网络列表不返回任何内容_.net_Vb.net - Fatal编程技术网

.net 类中的VB网络列表不返回任何内容

.net 类中的VB网络列表不返回任何内容,.net,vb.net,.net,Vb.net,我有一个带有一些变量的类,其中一个是(整数的)列表。 我不会把整个代码都放进去,因为它只在类中的列表中比较大 Dim excluir As New List(Of Integer) Public Sub New(taquilla As Integer, gallera As String, libras As Integer, onzas As Integer, puya As Integer, tuerto As Boolean, puntos As Integer, excluir

我有一个带有一些变量的类,其中一个是(整数的)列表。 我不会把整个代码都放进去,因为它只在类中的列表中比较大

    Dim excluir As New List(Of Integer)

Public Sub New(taquilla As Integer, gallera As String, libras As Integer, onzas As Integer, puya As Integer, tuerto As Boolean, puntos As Integer, excluir As List(Of Integer))
    Me.taquilla = taquilla
    Me.gallera = gallera
    Me.libras = libras
    Me.onzas = onzas
    Me.puya = puya
    Me.tuerto = tuerto
    Me.excluir = excluir
    Me.puntos = puntos
End Sub
Public Property excluirP As List(Of Integer)
    Get
        Return excluir
    End Get
    Set(value As List(Of Integer))
        excluir = value
    End Set
End Property
在我的表单中,我向列表“excluir”添加值

然后将值添加到对象

misGallos.Add(New Gallo(txtTaquilla.Text, txtGallera.Text, txtLibras.Text, txtOnzas.Text, txtPuya.Text, tuerto, txtPuntos.Text, excluir))
问题是,当我试图在对象中打印该列表的值时,它没有返回任何内容

    For i = 0 To excluir.Count
        For j = 0 To excluir.Count
            MsgBox(misGallos(i).excluirP.Item(j))
        Next
    Next

谢谢

你这样做完全是错误的。当您通过属性公开集合时,虽然您不一定非得公开,但通常应该将该属性设置为只读。通过这样做,您可以添加、删除和访问项目,但不允许替换列表本身,更糟糕的是,不允许删除和不替换列表本身。例如

Public Class SomeType

    Public ReadOnly Property Numbers As New List(Of Integer)

    '...

End Class
如果希望能够使用构造函数填充列表,则可以这样做,但不设置属性本身。获取属性,然后向其中添加项,例如

Public Class SomeType

    Public ReadOnly Property Numbers As New List(Of Integer)

    Public Sub New(numbers As IEnumerable(Of Integer))
        Me.Numbers.AddRange(numbers)
    End Sub

    '...

End Class
至于你的问题,你的循环没有意义。这:

For i = 0 To excluir.Count
    For j = 0 To excluir.Count
        MsgBox(misGallos(i).excluirP.Item(j))
    Next
Next
至少应该是这样的:

For i = 0 To misGallos.Count - 1
    For j = 0 To misGallos(i).excluirP.Count - 1
        MessageBox.Show(misGallos(i).excluirP(j).ToString())
    Next
Next
请注意,在上界中的值必须小于计数1。还请注意,如果使用
i
索引
misGallos
,则外部循环的上限应基于
misGallos
的计数,而不是
excluirP
。最重要的是,请注意,内部循环基于外部循环指示的当前项,而不是完全基于其他项

更好的做法是对每个循环使用
,而不是对
循环使用
。如果使用
For
循环计数器除了索引单个列表之外什么都不做,那么几乎可以肯定的是,应该为每个
循环使用

For Each m In misGallos
    For Each e In m.excluirP
        MessageBox.Show(e.ToString())
    Next
Next
再次注意,内部循环基于外部循环的当前项


此外,请使用大写字母作为属性名称的开头。

在我的表单中,我可能会在列表“excluir”中添加值,但事实上,您没有。当在表单中声明Dim excluir As New List(Of Integer)
时,您已经创建了该列表的一个全新实例,与另一个类中的实例完全分离。有关在两个类之间共享列表的一种方法,请参见建议的副本。请注意,可能还有其他更合适的方法,这取决于完整代码的实际外观。坦白地说,解释所有的可能性会使问题变得过于宽泛。@Craig,我确实这样做了。更正。
For Each m In misGallos
    For Each e In m.excluirP
        MessageBox.Show(e.ToString())
    Next
Next