Solved

Need help on "Contains" method...

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
COnsume rest client 6 36
Excel file not created as expected 7 50
SQL Exceptions 3 38
free scanner TWAIN can be operated with a Web application 9 39
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 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

17 Experts available now in Live!

Get 1:1 Help Now