Solved

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

Posted on 2006-11-25
8
421 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
[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
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 86

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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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 86

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 86

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

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

630 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