bbesase
bbesase

Reputation: 861

Passing a list into a method as a parameter

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.

Upvotes: 0

Views: 14063

Answers (1)

rwisch45
rwisch45

Reputation: 3702

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)

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

Upvotes: 3

Related Questions