P
Peter Gast
I need a function to get an item of a Dictionary randomly. Because Dicionary
hasn't an item-property like List (Of ..) I cannot pick the item with a
random index. The type of Dictionary can varies, so I have to declare it as
an object. It should also work for SortedDictionary.
The code above works, but I thinks object as type isn't good. Are there an
ideas for improvements?
Public Shared Function getElement(ByVal liste As Object) As Object
Dim position As Integer = CInt(Rnd() * (liste.Values.Count - 1))
Dim counter As Integer = 0
For Each item As Object In liste.values
If counter = position Then Return item
counter += 1
Next
End Function
Peter
hasn't an item-property like List (Of ..) I cannot pick the item with a
random index. The type of Dictionary can varies, so I have to declare it as
an object. It should also work for SortedDictionary.
The code above works, but I thinks object as type isn't good. Are there an
ideas for improvements?
Public Shared Function getElement(ByVal liste As Object) As Object
Dim position As Integer = CInt(Rnd() * (liste.Values.Count - 1))
Dim counter As Integer = 0
For Each item As Object In liste.values
If counter = position Then Return item
counter += 1
Next
End Function
Peter