Solved

VB.NET find value in list of values

Posted on 2008-10-15
10
4,241 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
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

828 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