?
Solved

VB.NET find value in list of values

Posted on 2008-10-15
10
Medium Priority
?
4,325 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:Howard Bash
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 86

Accepted Solution

by:
Mike Tomlinson earned 2000 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:Howard Bash
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 86

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
 
LVL 1

Author Comment

by:Howard Bash
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 86

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 86

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:Howard Bash
ID: 22725033
This works like a charm!!! I wish I could send you points again...

Thank you sir!
0
 
LVL 86

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

762 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