Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Checking if a variable is in a list of values

Posted on 2008-06-26
2
Medium Priority
?
800 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 48

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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

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 …
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

610 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