Solved

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

Posted on 2006-11-25
8
416 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 142

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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

947 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

18 Experts available now in Live!

Get 1:1 Help Now