将范围链接到集合中的项目

发布于 2024-11-30 02:44:16 字数 925 浏览 1 评论 0原文

我有一个这样的集合

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"
})

,并希望将 1 到 100 之间的十进制值链接到我的集合中的相应项目。

所以基本上我想要实现的目标就是这个。

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

但我确信一定有一种“更干净”的方式来做到这一点?

I have a 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??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡莣 2024-12-07 02:44:16

只需除以四舍五入即可得到索引。

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

一些测试点:

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)

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)
街角卖回忆 2024-12-07 02:44:16

将值除以 10,然后得到该值的下限,应该会给出适当的指数。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文