我要做的是检查一个值是否与两个数字中的一个匹配(并且很容易添加到要比较的数字)。而不是做一个啰嗦的方式,如:
If Number = 1 Or Number = 2 Then ...
我正在尝试做这样的事情:
If Number In (1,2) Then...
由于In
运算符在VB中不可用,我尝试使用以下代码:
Protected SectionID As Integer = HttpContext.Current.Request.QueryString("sectionid")
Protected PageID As Integer = HttpContext.Current.Request.QueryString("pageid")
Protected Sub HotspotsLV_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles HotspotsLV.ItemDataBound
Dim SecondLineHolder As HtmlControl = e.Item.FindControl("SecondLineHolder")
Select Case True
Case New String("2", "3").Contains(SectionID) : SecondLineHolder.Attributes("style") = "color:#21720B"
Case New String("8", "12").Contains(PageID) : SecondLineHolder.Attributes("style") = "color:#1B45C2"
End Select
End Sub
我发现这仅在SectionID
为2或PageID
为8时才有效。如果SectionID
为3或PageID
为12,则无效。为什么这是我可以做些什么来试图解决问题?感谢。
答案 0 :(得分:3)
经过一段时间的游戏,我找到了一个很好的解决方案:
Select Case True
Case Array.IndexOf(New Integer() {2, 3}, SectionID) > -1 : SecondLineHolder.Attributes("style") = "color:#21720B"
Case Array.IndexOf(New Integer() {8, 12}, PageID) > -1 : SecondLineHolder.Attributes("style") = "color:#1B45C2"
End Select
答案 1 :(得分:2)
Dim Numbers() As Integer = {1, 2}
If Numbers.Any(Function(i) i = Number) Then
答案 2 :(得分:1)
您正在创建String
实例而不是数组。尝试将其更改为:
Protected SectionID As Integer = HttpContext.Current.Request.QueryString("sectionid")
Protected PageID As Integer = HttpContext.Current.Request.QueryString("pageid")
Protected Sub HotspotsLV_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles HotspotsLV.ItemDataBound
Dim SecondLineHolder As HtmlControl = e.Item.FindControl("SecondLineHolder")
Dim sections As Integer() = New Integer(){2,3}
Dim pages As Integer() = New Integer(){8,12}
Select Case True
Case sections.Contains(SectionID) : SecondLineHolder.Attributes("style") = "color:#21720B"
Case pages.Contains(PageID) : SecondLineHolder.Attributes("style") = "color:#1B45C2"
End Select
End Sub
如果您使用Option Strict On
,则会突出显示类型不匹配。在您的初始代码中,New String("2", "3")
会创建一个值为222
的字符串。
对于3.5之前的.Net版本,Contains
方法将不可用。这可以使用IndexOf
:
Array.IndexOf(sections, SectionID) > -1
' Equivalent to sections.Contains(SectionID)