Search code examples
asp.netvb.netcomparison-operators

Equivalent of SQL IN in VB.NET


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.


Solution

  • 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