将列表作为参数传递给方法

时间:2013-07-09 17:15:32

标签: vb.net list parameters

我有两个功能,一个是:

Public Sub PopulateValidKeystrokeList()
    ValidKeystrokeList.Add(Keys.A)
    ValidKeystrokeList.Add(Keys.B)
    ValidKeystrokeList.Add(Keys.C)
    ValidKeystrokeList.Add(Keys.D)
    ValidKeystrokeList.Add(Keys.E)
    ValidKeystrokeList.Add(Keys.F)
    ValidKeystrokeList.Add(Keys.G)
    ValidKeystrokeList.Add(Keys.H)
    ValidKeystrokeList.Add(Keys.I)
    ValidKeystrokeList.Add(Keys.J)
    'backPopulateDictionary()
End Sub

另一个是这个:

Public Function backPopulateDictionary(ByRef validkeylist As List(Of String))
    For i As Integer = 0 To ValidKeystrokeList.Count - 1
        keystrokeDictionary.Add(ValidKeystrokeList.Item(i), i)
    Next
    Return keystrokeDictionary
End Function

我需要将整个列表传递给backPopulateDictionary(),但我不确定如何将整个列表作为参数传递,因为到目前为止我所尝试的所有内容都只是出错并且无效。

1 个答案:

答案 0 :(得分:2)

Public Function backPopulateDictionary(ByVal validkeylist As List(Of String))
    For i As Integer = 0 To validkeylist.Count - 1
        keystrokeDictionary.Add(validkeylist.Item(i), i)
    Next
    Return keystrokeDictionary
End Function

并使用backPopulateDictionary(ValidKeystrokeList)

进行调用

validkeylist是参数,因此当您添加到keystrokeDictionary时,您需要使用参数名称 - 而不是实际列表的名称。