开发者

Linking ranges to item in collection

I have a开发者_C百科 collection as such

Private Shared ReadOnly thermoPaths As New ReadOnlyCollection(Of String) _
({
   "thermometer_000_108x320.jpg",
   "thermometer_010_108x320.jpg",
   "thermometer_020_108x320.jpg",
   "thermometer_030_108x320.jpg",
   "thermometer_040_108x320.jpg",
   "thermometer_050_108x320.jpg",
   "thermometer_060_108x320.jpg",
   "thermometer_070_108x320.jpg",
   "thermometer_080_108x320.jpg",
   "thermometer_090_108x320.jpg",
   "thermometer_100_108x320.jpg"
})

And wish to link a decimal value between 1 and 100 to the corresponding item in my collection.

So basically what I am trying to achieve is this.

Select Case Decimal.Round(value)
    Case 1 To 9
        Dim x As String = thermoPaths(0)
    Case 10 To 19
        Dim x As String = thermoPaths(1)
    Case 20 To 29
        Dim x As String = thermoPaths(2)
    Case 30 To 39
        Dim x As String = thermoPaths(3)
    case ''AND SO ON
End Select

But I'm sure there must be a "cleaner" way of doing this??


Just divide and round up to get the index.

i = Math.Ceiling(value / 10)
If value >= 0 AndAlso i < thermoPaths.Count Then
    path = thermoPaths(i)
Else
    path = Nothing
End If

Some test points:

value   i   path
 -1     0   <null> (because value < 0)
  0     0   thermometer_000_108x320.jpg
  1     1   thermometer_010_108x320.jpg
  5     1   thermometer_010_108x320.jpg
 10     1   thermometer_010_108x320.jpg
 49     5   thermometer_050_108x320.jpg
 51     6   thermometer_060_108x320.jpg
 90     9   thermometer_090_108x320.jpg
 91    10   thermometer_100_108x320.jpg
100    10   thermometer_100_108x320.jpg
101    11   <null> (because i > 10)


Divide value by 10, then get the floor of that, should give you the appropriate index.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜