?
Solved

Compare array list to a string, asp.net, c#

Posted on 2009-02-20
4
Medium Priority
?
926 Views
Last Modified: 2013-12-17
Hi,

I have the following code

 while (rdrEUrls.Read())
                            {
                                string requrls = rdrEUrls["requrl"].ToString();
                                GetalldUrlsFromDase.Add(requrl);


                            }

where GetalldUrlsFromDase is an array list

what i want to do is i have a string called X which has a value I want to loop through the whole array list and check if the value of x is in the array list. if it is then do nothing if it isnt then lbltext="Something there"

please help.

Thanks.

R8VI
0
Comment
Question by:R8VI
  • 2
4 Comments
 
LVL 15

Expert Comment

by:angus_young_acdc
ID: 23690428
Try this (assuming your arraylist is full of strings, if not just do .To.String())
            string X = "The string you want to check for";
            foreach (string obj in myArrayListName)
            {
                if (X == obj)
                {
                    // do whatever processing you wish
                }
            }

Open in new window

0
 
LVL 18

Accepted Solution

by:
Richard Lee earned 2000 total points
ID: 23690452
Below are two options
// Option 1
int index = alst.BinarySearch(X, delegate(object a, object b) { return String.Compare(a, b, true); });
 
if (index < 0)
{
    lbltext="Something there"
}
 
// Option 2
bool found = false;
foreach(string item in alst)
{
    if (String.Compare(X, item, true) == 0)
    {
        found = true;
        break;
    }
}
 
if (!found)
{
    lbltext="Something there"
}

Open in new window

0
 
LVL 18

Expert Comment

by:Richard Lee
ID: 23690456
First two lines now included
ArrayList alst = new ArrayList();
string X = "a string";
 
// Option 1
int index = alst.BinarySearch(X, delegate(object a, object b) { return String.Compare(a, b, true); });
 
if (index < 0)
{
    lbltext="Something there"
}
 
// Option 2
bool found = false;
foreach(string item in alst)
{
    if (String.Compare(X, item, true) == 0)
    {
        found = true;
        break;
    }
}
 
if (!found)
{
    lbltext="Something there"
}

Open in new window

0
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 23690525
Don't use ArrayList at all, it's practically obsolete. Use a generic list of strings:

List<string> urls = new List<string>();
while (rdrEUrls.Read()) {
   string requrls = rdrEUrls["requrl"].ToString();
   urls.Add(requrl);
}

Now you don't have to loop through the list yourself to look for a value, there are ready made methods for that:

if (!urls.Contains(X)) {
   lbltext="Something there"
}

If you are doing a lot of lookups in the list, you could use a dictionary instead, as it's a lot faster to look for a key in a dictionary:

Dictionary<string,int> urls = new Dictionary<string,int>();
while (rdrEUrls.Read()) {
   string requrls = rdrEUrls["requrl"].ToString();
   if (!url.ContainsKey(requrl)) {
      urls.Add(requrl, 0);
   }
}

Now checking for a string is practically instant, even if you would have a million strings in the dictionary.

if (!urls.ContainsKey(X)) {
   lbltext="Something there"
}
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

839 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