?
Solved

Checking if a variable is in a list of values

Posted on 2008-06-26
2
Medium Priority
?
799 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
[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
2 Comments
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 2000 total points
ID: 21873827
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
ID: 21874491
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

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!

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month13 days, 15 hours left to enroll

801 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