Brandon Lewis

Brandon Lewis

  • NA
  • 527
  • 125.7k

CompareTo() With Generic List of Classes

Dec 11 2007 10:59 AM
I have a class that is built like this:

private class Site
{
    private string name;
    private string uniqueID;

    internal string Name
    {
       get { return this.name; }
       set { this.name = value; }
    }
    internal string UniqueID
    {
       get { return this.uniqueID; }
       set { this.uniqueID = value; }
    }

    public int CompareTo(object site)
    {
       return String.Compare(this.uniqueID, ((Site)site).uniqueID);
    }
}


And in another class, I have a generic list of these Site classes.

public sealed class SiteList
{
    private List<Site> pendingSiteList = new List<Site>;

    public bool IDExists(string uniqueID)
    {
       //This is where the problem lays
       int index = pendingSiteList.BinarySearch();

       if(index >= 0)
          return true;
       else
          return false;
    }

    public List<Site> PendingSiteList
    {
       get { return this.pendingSiteList; }
    }
}


I supposed the main problem is that I want to search the generic site lists to find out if a specified unique ID exists in any of the contained sites. But therin lay a problem in that the binary search method of a the generic list is expecting data of the type "Site" (strongly typed I suppose...), not string. I need to search this list by unique ID if thats possible. Im writing this code (and color coding it too ;) ) off the top of my head while at work so it may not be entirely accurate but I tried to make sure it was valid. Any help is much appreciated :)

Answers (2)