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

x
?
Solved

C# If Item is not in array

Posted on 2008-06-17
2
Medium Priority
?
2,096 Views
Last Modified: 2013-12-16
Hello,

What I want to be able to do, in C#, is not do something if the item is in an array.

I have a for loop going through a list of items, and it is modifying them. There are some of them that I don't want modified, and I have stored the text of them in a pre-defined array. What I want, is the for loop to go through the array of elements that I want modified, but if the text matches any of the items in another array that I have defined, to not do anything.

Is there any easy way to do this?

Thanks.
0
Comment
Question by:dh-s
[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 13

Expert Comment

by:josgood
ID: 21810001
If you make a generic List of don't modify strings as in
   List<string> dontModify= new List<string>();

and then add each text string that shouldn't be modified to the List
      dontModify.Add("nochange1");
      dontModify.Add("nochange2");

then you can
   struct Item {
      public string someText;
   };
   Item myItem = new Item(); // you'll have to initialize it correctly, of course
   if (dontModify.Contains(myItem.someText)) Console.WriteLine("Don't change");

0
 

Accepted Solution

by:
shman911 earned 750 total points
ID: 21810094
iosgood's solution works if you're using a List.  But if you must keep those strings in an array, then you can use the System.Array.Sort() and then System.Array.BinarySearch().  See the example below.  BinarySearch will return an int equal to or greater than zero if the string is found in the array, and will return a negative number if not found.

The only downside is that Sort() will change the ordering of the strings in the array.  If the ordering is important, then you can't sort, and therefore you can't use BinarySearch.
 


string [] strArr = new string[] {"peter", "fred", "bob"};
 
System.Array.Sort(strArr);
System.Array.BinarySearch(strArr, "bob"); //returns int >= 0
System.Array.BinarySearch(strArr, "eric"); //returns int < 0

Open in new window

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

636 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