[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Need help on "Contains" method...

Posted on 2009-07-08
3
Medium Priority
?
221 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 1400 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 600 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

656 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