在Vb.net应用程序中通过引用传递列表的项目

时间:2014-10-30 14:09:01

标签: .net vb.net winforms collections reference

我想更改布尔列表的内容,所以我通过引用将它传递给方法,如下所示:

 Dim liste As New List(Of Boolean)
    liste.Add(lbl1.Visible)
    liste.Add(lbl2.Visible)
    RaiseEvent rgProcS9Selection(liste, SelectedIndex)
方法中的

    Private Sub rgProcS9SelectionMethod(ByRef ComposantsVisibility As List(Of Boolean), ByVal SelectedIndex As Integer)
            ComposantsVisibility.Clear()
            If SelectedIndex = 0 Then
                For index As Integer = 0 To 1
                    ComposantsVisibility.Add(False)
                Next
            Else
                For index As Integer = 0 To 11
                    ComposantsVisibility.Add(True)
                Next
            End IF
End Sub

列表的内容值已更改,但标签的可见性(lbl1,lbl2)仍然相同。

  1. 所以,我需要知道为什么没有通过引用传递这些项目?
  2. 如何修复我的代码

1 个答案:

答案 0 :(得分:1)

您需要two个列表。一个用于可见性,一个用于标签

Dim liste As New List(Of Boolean)
Dim listeL As New List(Of Label)
liste.Add(lbl1.Visible)
liste.Add(lbl2.Visible)
listeL.Add(lbl1)
listeL.Add(lbl2)
RaiseEvent rgProcS9Selection(liste, listeL, SelectedIndex)

并在rgProcS9SelectionMethod

的末尾
Private Sub rgProcS9SelectionMethod(ByVal ComposantsVisibility As List(Of Boolean), ByVal ComposantsLabels As List(Of Label), ByVal SelectedIndex As Integer)
    ...
    ...
    For i = 0 To ComposantsLabels.Count - 1
        ComposantsLabels(i).Visible = ComposantsVisibility(i)
    Next
End Sub