Reputation: 2607
What I am trying to do is to check if a value matches one of two numbers (and easily be able to add to the numbers to compare to). Rather than doing a longer-winded way such as:
If Number = 1 Or Number = 2 Then ...
I'm trying to do something like this:
If Number In (1,2) Then...
As the In
operator isn't available in VB, I have tried the following code instead:
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
I have found that this only works when SectionID
is 2 or PageID
is 8. If SectionID
is 3 or PageID
is 12 then it doesn't work. Why is this and what can I do to try to get around the problem? Thanks.
Upvotes: 4
Views: 2610
Reputation: 7280
You are creating a String
instance not an array. Try changing it to:
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
If you use Option Strict On
then the type mismatch would be highlighted. In your initial code New String("2", "3")
would create a string with a value of 222
.
For .Net version prior to 3.5 the Contains
method will not be available. This can be mimicked using IndexOf
:
Array.IndexOf(sections, SectionID) > -1
' Equivalent to sections.Contains(SectionID)
Upvotes: 1
Reputation: 2607
After a bit of playing around, I have managed to find a nice solution:
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
Upvotes: 3
Reputation: 415860
Dim Numbers() As Integer = {1, 2}
If Numbers.Any(Function(i) i = Number) Then
Upvotes: 2