0

I have bunch of usernames in this arraylist and I want to check whether username exists in the arraylist or not but the method always returns false.

public bool check_username(ArrayList userList, string username)
{           
    for (int i = 0; i < userList.Count; i++)
    {
        if (userList[i].ToString() == username)
        {
            return true;
        }
    }
    return false;
} 
ChrisWue
  • 18,612
  • 4
  • 58
  • 83
DeadlyDagger
  • 599
  • 4
  • 12
  • 28
  • 1
    Post the values in userList and username. – BNL Nov 26 '11 at 19:04
  • 3
    Why are you using `ArrayList` instead of `List`? – Oded Nov 26 '11 at 19:05
  • try if (userList[i].ToString().EqualsTo(username)) – juergen d Nov 26 '11 at 19:08
  • 1
    Is the ToString() method returning the username of the user, oder the class-name? Try userList[i].GetUserName() instead – schoetbi Nov 26 '11 at 19:09
  • Have you considerd Linq for this? var user = userlist.FirstOrDefault(u => u.Name == userName); – schoetbi Nov 26 '11 at 19:11
  • It is unclear what `userList` contains. Strings? Objects of some User class? Use `List` or `List` instead of `ArrayList`. If you still want to stick to `ArrayList` use casting `(string)userList` instead of `ToString()`. This will generate a runtime error if the list doesn't contain strings and thus give you a useful hint. Or at least override the `ToString` method in your `User` class and have it return the user name. – Olivier Jacot-Descombes Nov 26 '11 at 19:21
  • possible duplicate of [Searching array is not returning any results](http://stackoverflow.com/questions/8280758/searching-array-is-not-returning-any-results) – H H Nov 26 '11 at 19:38

3 Answers3

4

Consider making your string comparison case insensitive.

username.Equals(userList[i].ToString(), StringComparison.OrdinalIgnoreCase);

Or, assuming all elements of your ArrayList userList are strings, and you're using .NET 3.5 or later, you can simplify this using LINQ:

public bool check_username(ArrayList userList, string username)
{ 
    return userList.Cast<string>()
           .Any(s => s.Equals(username, StringComparison.OrdinalIgnoreCase); 
} 
Adam Rackis
  • 82,527
  • 56
  • 270
  • 393
1

Without seeing your list or what you are passing we can't know for sure, but it could be a normalization problem.

Bob, bob, BOB, BOb etc... are not the same when comparing strings.

Replace your if statement with this:

if(userList[i].ToString().ToLower() == username.ToLower())
{
     return true;
}
Johnie Karr
  • 2,744
  • 2
  • 35
  • 44
1

There are several reasons I could think of that could cause that function to always return false.

  1. Are you sure that your userList and username will always have the same casing? It would be a good practice to use .ToLower() or .ToUpper() to insure that the casing matches unless you intend for casing to be part of the match.

  2. Are you sure there is no extra whitespace on either string? It is good practice to use .Trim() when you are comparing strings where there may be extra whitespace.

  3. Using the .Equals() method when comparing string is more reliable than the logical operator ==. Occasionally the logical operator produces incorrect results.

  4. Are you positive that you should be getting a true result? Is it possible one string contains a hidden character that you are unaware of? Use the debugger to check the values of the strings to be sure.

Nick Zimmerman
  • 1,471
  • 11
  • 11
  • If you want to do case insensitive comparison, `String.Equals(a, b, StringComparison.CurrentCultureIgnoreCase)` is a good way to do it. – Mike Bailey Nov 26 '11 at 19:13