For Each Dictionary loop in Index order

后端 未结 3 789
一向
一向 2020-12-22 07:41

I have tried my hand using for loop with Dictionary but couldn\'t really achieve what I want to.

I have a certain variable SomeVariab

3条回答
  •  佛祖请我去吃肉
    2020-12-22 08:36

    You can't access the dictionary by index but you can access the keys collection by index. You don't need a loop for this at all.

    So something like this.

    If SomeVariable = 1 Then
        Return SomeCollection(SomeCollection.Keys(2))
    ElseIf SomeVariable = 2 Then 
        ...
    End If
    

    If it is truly structured you could do this:

    Return SomeCollection(SomeCollection.Keys((SomeVariable * 3) - 1))
    

    You probably need some error checking and ensuring that the length of the dictionary is correct but this should put you on the right track.

提交回复
热议问题