Solved

VB.NET find value in list of values

Posted on 2008-10-15
10
4,231 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Variable Event ? 3 29
SQL LINE CONTINUATION ISSUE 12 33
Help with exporting to excel 4 28
Sending receiving text messages in vb.net 15 38
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

821 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