Solved

VB.NET find value in list of values

Posted on 2008-10-15
10
4,201 Views
Last Modified: 2012-05-05
In SQL,  one can construct a query as follows:
  select * from someTable where someTable.ColToLookAt in ('Foo', 'Bar',  'etc')

is there any construct that would allow me to do something like:

if varName in {"A", "B". "ETC"}  then
  'do back flips
end if


0
Comment
Question by:hbash
10 Comments
 
LVL 37

Expert Comment

by:samtran0331
ID: 22722485
one quick way would be a select case...

select case varName
case "A","B","C"
'do backflips
case "D","E"
'do somersaults
case else
'stand still
end select
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 22722573
The only way I know to do this is to iterate the list and check each value.

I saw a solution by TheLearnedOne that was supposed to do that ... though I have to think that behind the scenes it's also iterating the list.  Can't find that question now though ...


0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 22722853
How about?

        Dim varName As String = "B"
        If Array.IndexOf(New String() {"A", "B", "ETC"}, varName) <> -1 Then
            'do back flips
        End If
0
 
LVL 1

Author Comment

by:hbash
ID: 22723926
Problem with this solution:
given this version -
            Dim varName As String = "BREC"
            If Array.IndexOf(New String() {"A", "B", "C"}, varName) <> -1 Then
                MsgBox("Got It")
            End If
the "B" in "BREC" is not detected.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22723954
Ah...you are going the opposite of the way I went...

My snippet was seeing if VarName was in the List.

You want to know if any of the value ins the List exist in VarName.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 1

Author Comment

by:hbash
ID: 22724051
Exactly.  

Without boring you with the details,  I have an array of record strings and an array of record types.
I need to iterate through my array of record strings looking for "some" of the allowable record types to allow the user to filter the displayed records with a "filter" criteria.  

Currently I use two loops to cull the subset of records to view,  which can be quite slow.

Any thoughts on an approach?  

Thanks,
Howard
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22724088
I think the others are right in saying there isn't any way to do it except to iterate...   =\

It sounds like your problem is more complicated, but would a helper function be of use?
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim varName As String = "BREC"

        If ContainsAny(varName, New String() {"A", "B", "C"}) Then

            MsgBox("Got It")

        End If

    End Sub
 

    Private Function ContainsAny(ByVal str As String, ByVal values() As String) As Boolean

        For Each value As String In values

            If str.Contains(value) Then

                Return True

            End If

        Next

        Return False

    End Function

Open in new window

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22724157
If you are looking for SINGLE CHARACTERS only, then you can use String.IndexOfAny():
http://msdn.microsoft.com/en-us/library/system.string.indexofany(VS.80).aspx

        Dim varName As String = "BREC"
        If varName.IndexOfAny("ABC".ToCharArray) <> -1 Then
            MsgBox("Got It")
        End If
0
 
LVL 1

Author Comment

by:hbash
ID: 22725033
This works like a charm!!! I wish I could send you points again...

Thank you sir!
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22725254
More points?...that wouldn't be right!

I'd say since my original answer wasn't really what you wanted...I've only earned the points by posting after the question was already closed.  ;)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now