Solved

Checking if a variable is in a list of values

Posted on 2008-06-26
2
793 Views
Last Modified: 2008-06-26
Is there a quicker way to check if a String variable in VB 2005 is in a list of values? For example, instead of the following:

Dim x As String
If x = "Mr" Or x = "Mrs" Or x = "Miss" Or x = "Ms" Or x = "Prof" Or x = "Doc" Then
    'Do something
End If

Isn't there something like this?:

Dim x As String
Dim values() As String = {"Mr", "Mrs", "Miss", "Ms", "Prof", "Doc"}
If x IsIn values Then
    'Do something
End If

Something along those lines?

Thanks
0
Comment
Question by:SETP
2 Comments
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 500 total points
Comment Utility
Use Array.IndexOf....
        Dim x As String

        Dim values() As String = {"Mr", "Mrs", "Miss", "Ms", "Prof", "Doc"}

        If Array.IndexOf(values, x) >= 0 Then

            MsgBox("Exists")

        Else

            MsgBox("Not Exist")

        End If

Open in new window

0
 
LVL 27

Expert Comment

by:planocz
Comment Utility
You can also play around with regexpressions....
If you break on fieldValues  you can see the filed break down of this item.
from there you can look for different string item that you want.

Sample:
       Dim strTest As String = "Dr. Jane O Smith M.D."
        Dim fieldValues As String() = ParseLine(strTest)

    End Sub
    Private Shared Function ParseLine(ByVal oneLine As String) As String()
        Dim pattern As String = "[ ,]+(?=(?:[^""]*""[^""]*"")*(?![^""]*""))"
        Dim r As System.Text.RegularExpressions.Regex = _
                New System.Text.RegularExpressions.Regex(pattern)
        Return r.Split(oneLine)
    End Function
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

763 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

15 Experts available now in Live!

Get 1:1 Help Now