R8VI
asked on
Compare array list to a string, asp.net, c#
Hi,
I have the following code
while (rdrEUrls.Read())
{
string requrls = rdrEUrls["requrl"].ToStrin g();
GetalldUrlsFromDase.Add(re qurl);
}
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
I have the following code
while (rdrEUrls.Read())
{
string requrls = rdrEUrls["requrl"].ToStrin
GetalldUrlsFromDase.Add(re
}
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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"
}
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"].ToStrin g();
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"].ToStrin g();
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"
}
List<string> urls = new List<string>();
while (rdrEUrls.Read()) {
string requrls = rdrEUrls["requrl"].ToStrin
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"].ToStrin
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"
}
Open in new window