Solved

VB.NET find value in list of values

Posted on 2008-10-15
10
4,293 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 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

707 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