I have tried my hand using for
loop with Dictionary
but couldn't really achieve what I want to.
I have a certain variable SomeVariable
and on the value of this variable I want my foreach
to work. SomeVariable
can be 1,2,3 or 4
So lets say SomeVariable
is 1
I want to retrieve the last item.value
from among the first 3 indexes(0,1,2) inside the SomeCollection
.
And if SomeVariable
is 2
I want to retrieve the last item.value
from among the next 3 indexes(3,4,5) inside the SomeCollection
.
And so on...
For Each item As KeyValuePair(Of String, Integer) In SomeCollection
If SomeVariable = 1 Then
//....
ElseIf SwitchCount = 2 Then
//....
End If
Next
You can always use a generic SortedDictionary, I only use C# so here's my example:
A dictionary has no defined order, so any order you perceive is transient. From MSDN:
Trying to use the Keys collection to determine the order shows how it is transient:
the output prints 0 - 8, in order, as you might expect. then:
The output is: 0, 1, 2, 3, 4, 9 (!), 6, 7, 8
As you can see, it reuses old slots. Any code depending on things to be in a certain location will eventually break. The more you add/remove, the more unordered it gets. If you need an order to the
Dictionary
useSortedDictionary
instead.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 it is truly structured you could do this:
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.