Solved

Checking if a variable is in a list of values

Posted on 2008-06-26
2
796 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 500 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.net and sql server 4 54
get combo value in class 5 24
How to duplicate form in Visual Studio 2015 2 96
ASP.NET/VB: Convert Date and Time to YYYY-MM-DDTHH:MM:SS 3 32
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 …
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

740 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