Solved

Need help on "Contains" method...

Posted on 2009-07-08
3
218 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.NET - Why is accordion not working? 3 68
Create a Dropdown list with sub group 10 43
Web page design problem 3 41
Returning multiple values with a stored procedure 9 31
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Suggested Courses

739 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