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 :)
Loading
Posted Dec 11, 2007, 1:09 PM
The whole idea behind this is to try to avoid recasts if at all possible (I have a feeling that theyre dubiously expensive). I want to be able to have the list already filled with precast Site objects so I dont have to have access to the actual Site class in the upper class heirarchy in order to be able to use the items in the list and I can avoid recasts. This will work wonderully!
Thanks :)
AlanPosted Dec 11, 2007, 12:58 PM
It needed some modification but try this, complete with some (rather silly) test code:
using System;
using System.Collections.Generic;
class Test
{
static void Main()
{
Site s1 = new Site();
Site s2 = new Site();
Site s3 = new Site();
Site s4 = new Site();
Site s5 = new Site();
s1.UniqueID = "2";
s1.Name = "Google";
s2.UniqueID = "3";
s2.Name = "C# Corner";
s3.UniqueID = "1";
s3.Name = "Microsoft";
s4.UniqueID = "4";
s4.Name = "Wikipedia";
s5.UniqueID = "5";
s5.Name = "Code Project";
List sites = new List();
sites.Add(s1);
sites.Add(s2);
sites.Add(s3);
sites.Add(s4); // note s5 not added
SiteList sl = new SiteList(sites);
bool b = sl.IDExists("3");
if (b)
{
Console.WriteLine("C# Corner is in the list");
}
else
{
Console.WriteLine("C# Corner is not in the list");
}
b = sl.IDExists("5");
if (b)
{
Console.WriteLine("Code Project is in the list");
}
else
{
Console.WriteLine("Code Project is not in the list");
}
Console.ReadKey();
}
}
public class Site : IComparable
{
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);
}
}
public sealed class SiteList pendingSiteList = new List();
{
private List
public bool IDExists(string uniqueID)
{
Site site = new Site();
site.UniqueID = uniqueID;
int index = pendingSiteList.BinarySearch(site);
if(index >= 0)
return true;
else
return false;
}
public List PendingSiteList
{
get { return this.pendingSiteList; }
}
public SiteList(List sites)
{
sites.Sort(); // needs to be sorted to use BinarySearch
pendingSiteList = sites;
}
}