Solved

Need help on "Contains" method...

Posted on 2009-07-08
3
219 Views
Last Modified: 2013-11-05
Hi,

Can someone please tell me why I need the "System.Linq" for using the "Contains" method? Please see code below. My company wants me to get rid of the line "using System.Linq;", but everytime I remove the line, I'll get an error on the "Contains" method. Is there a different way to accomplish the same thing that the "Contains" method is doing without using Ling?

Thanks very much in advance.

using System.Linq;
 
string divCode = "TWPP";
string[] divCodeArr1 = { "TEB", "TWB", "VSGTB" };
string[] divCodeArr2 = { "TEPP", "TWPP", "VSGTPP" };
 
if (divCodeArr1.Contains(divCode))
{
    //do something here
}
else if (divCodeArr2.Contains(divCode))
{
    //do something here
}
else
{
    //do something here
}

Open in new window

0
Comment
Question by:WebAppDeveloper
[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
3 Comments
 
LVL 15

Accepted Solution

by:
crisco96 earned 350 total points
ID: 24807424
You'll need to loop through each record in the array instead.  Here's a quick example
public void SomeFunction()
{
string divCode = "TWPP";
string[] divCodeArr1 = { "TEB", "TWB", "VSGTB" };
string[] divCodeArr2 = { "TEPP", "TWPP", "VSGTPP" };
 
if (Contains(divCode, divCodeArr1))
{
    //do something here
}
else if (divCodeArr2.Contains(divCode, divCodeArr2))
{
    //do something here
}
else
{
    //do something here
}
}
 
public bool Contains(string str, string[] arr)
{
   foreach(string val in arr)
   {
      if(str == val)
      {
         return true;
      }
   }
 
   return false;
}

Open in new window

0
 
LVL 41

Expert Comment

by:graye
ID: 24808249
To answer your specific question... the Contains() method is called an "extension method"...  although it's not normally considered a part of LINQ, it is a foundational element that is required for LINQ to function.
If you curious, take a look at the following:  http://en.wikipedia.org/wiki/Extension_method
0
 
LVL 7

Assisted Solution

by:Abu Hamdan
Abu Hamdan earned 150 total points
ID: 24808396
You could use arrayList instead of string arrays as follows:
System.Collections.ArrayList ss new System.Collections.ArrayList;
            ss.Add("item1"); 
            ss.Add("item2");
 
           if( ss.Contains("item1")
{
//whatever
} 

Open in new window

0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

691 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