Solved

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

Posted on 2006-11-25
8
415 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

9 Experts available now in Live!

Get 1:1 Help Now