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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17554315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:12:29  来源:igfitidea点击:

Passing a list into a method as a parameter

vb.netlistparameters

提问by bbesase

I have 2 functions, one is:

我有两个功能,一个是:

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

And the other is this:

另一个是这样的:

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

I need to pass the whole list into backPopulateDictionary()but I'm not sure how to pass a whole list as a parameter, since everything I have tried so far just errors out and doesn't work.

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

回答by rwisch45

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

And call it with backPopulateDictionary(ValidKeystrokeList)

并调用它 backPopulateDictionary(ValidKeystrokeList)

validkeylistis the parameter, so when you are adding to your keystrokeDictionary, you need to use the parameter name - not the name of the actual list.

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