Solved

Need an If statement to match text within any part of an array

Posted on 2006-11-25
8
419 Views
Last Modified: 2010-04-16
This is what I have and it works, but there must be an easier way:

            string[] aSharpKey = { "A#", "B#", "C##", "D#", "E#", "F##", "G##", "A#" };
            string User1 = UserChord1.Text;

            if (aSharpKey[0] == User1 || aSharpKey[1] == User1 || aSharpKey[2] == User1 || aSharpKey[3] == User1 || aSharpKey[4] == User1 || aSharpKey[5] == User1 || aSharpKey[6] == User1 || aSharpKey[7] == User1)
{
 //stuff
}

Is there syntax for searching the array to match any element, like:  if(aSharpKey[0 thru 7] == User1)
0
Comment
Question by:coolrazor
8 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18012030

string[] aSharpKey = { "A#", "B#", "C##", "D#", "E#", "F##", "G##", "A#" };
string User1 = UserChord1.Text;
string found = Array.Find(aSharpKeyUser1);

if (found == User1 )
{
 //stuff
}
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18012126
Find() is new in .Net 2.0.

You can also use the old IndexOf() method:
http://msdn2.microsoft.com/en-US/library/7eddebat(VS.80).aspx

0
 

Author Comment

by:coolrazor
ID: 18012172
I want to avoid using .Net 2.0
If possible, I'd like to avoid adding more strings or arrays before the "if" unless it simplifies this problem a lot.

Here's my latest try:

            string[] aSharpKey = { "A#", "B#", "C##", "D#", "E#", "F##", "G##", "A#" };
            string User1 = UserChord1.Text;

if (User1 == Array.IndexOf(aSharpKey, User1) )  //Error      1      Operator '==' cannot be applied to operands of type 'string' and 'int'
{
//stuff
}
0
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!

 

Author Comment

by:coolrazor
ID: 18012247
I'll bite the bullet and use .Net if it'll make my life way easier, but the syntax below didn't work:

string[] aSharpKey = { "A#", "B#", "C##", "D#", "E#", "F##", "G##", "A#" };
string User1 = UserChord1.Text;
string found = Array.Find(aSharpKeyUser1); // the part inside the ( ) isn't right; I tried dots, commas, string, etc.  What's missing?

if (found == User1 )
{
 //stuff
}
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18012261
Should be something like...

    int index = Array.Find(aSharpKey, User1);
    if (index != -1)
    {
        // "index" is the index it was found out
    }
    else
    {
        // the value was not found in the array
    }
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18012273
Sorry...that should read:

    // "index" is the index it was found AT
0
 
LVL 18

Accepted Solution

by:
Ravi Singh earned 30 total points
ID: 18012433
If you choose to stay with .NET 1.1 then one approach is to use the ArrayList.Adapter method to use the ArrayList method Contains():

string[] aSharpKey = { "A#", "B#", "C##", "D#", "E#", "F##", "G##", "A#" };
string User1 = UserChord1.Text;

if (ArrayList.Adapter(aSharpKey).Contains(User1))
{
      //Found
}
0
 

Author Comment

by:coolrazor
ID: 18012956
I finally went Zephyr's approach, but had to add a little in the begining:

if(System.Collections.ArrayList.Adapter(aSharpKey).Contains(User1))  
{
//Stuff
}
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

679 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