G
Guest
//////////////////////
// What I want to do
- Binary search a collection (ArrayList) consisting of objects that
implement IComparable.
- I want IComparable to respond differently depending on the class of object
I'm searching for
- eg.
ArrayList ClusterChains = new ArrayList();
ClusterChains.Add(new Cluster(new FileEntry("Test.txt", 1234)));
// etc.
// I want to be able to do all of the following
ClusterChains.BinarySearch(new Cluster(new FileEntry("Test.txt", 1234)));
ClusterChains.BinarySearch("Test.txt");
ClusterChains.BinarySearch(1234);
//////////////////////
// What I have done to achieve above - Is this ok
// bah these message editing boxes are too small
- Implemetn CompareTo et al for IComparable
- CompareTo responds differently depending on the class of object passed in
- Equals has been overrided but only operates on instances of the same class
ie. // looks like Cluster is actually ClusterChain..
public int CompareTo(object ProvidedObject)
{
int iResult = int.MinValue;
if (ProvidedObject is CFATEntryChain)
{
CFATEntryChain AnotherEntryChain = (CFATEntryChain) ProvidedObject;
iResult =
this.mFATEntry.uCluster.CompareTo(AnotherEntryChain.mFATEntry.uCluster);
}
else if (ProvidedObject is uint)
{
uint uAnotherCluster = (uint) ProvidedObject;
iResult = this.mFATEntry.uCluster.CompareTo(uAnotherCluster);
}
else if (ProvidedObject is string)
{
string sAnotherFileName = (string) ProvidedObject;
iResult = this.mFATEntry.sFileName.CompareTo(sAnotherFileName);
}
else
{
throw(new ArgumentException("CFATEntryChain.CompareTo - Exception,
compared parameter is not a CFATEntryChain"));
}
return iResult;
}
AND
public bool Equals(CFATEntryChain rhs)
{
if (rhs != null)
return this.CompareTo(rhs) == 0;
else
return false;
}
public override bool Equals(object obj)
{
if (obj != null)
{
if((obj is CFATEntryChain))
return this.Equals((CFATEntryChain) obj);
else
return false;
}
else
{
return false;
}
}
Cheers
Steve
// What I want to do
- Binary search a collection (ArrayList) consisting of objects that
implement IComparable.
- I want IComparable to respond differently depending on the class of object
I'm searching for
- eg.
ArrayList ClusterChains = new ArrayList();
ClusterChains.Add(new Cluster(new FileEntry("Test.txt", 1234)));
// etc.
// I want to be able to do all of the following
ClusterChains.BinarySearch(new Cluster(new FileEntry("Test.txt", 1234)));
ClusterChains.BinarySearch("Test.txt");
ClusterChains.BinarySearch(1234);
//////////////////////
// What I have done to achieve above - Is this ok
// bah these message editing boxes are too small
- Implemetn CompareTo et al for IComparable
- CompareTo responds differently depending on the class of object passed in
- Equals has been overrided but only operates on instances of the same class
ie. // looks like Cluster is actually ClusterChain..
public int CompareTo(object ProvidedObject)
{
int iResult = int.MinValue;
if (ProvidedObject is CFATEntryChain)
{
CFATEntryChain AnotherEntryChain = (CFATEntryChain) ProvidedObject;
iResult =
this.mFATEntry.uCluster.CompareTo(AnotherEntryChain.mFATEntry.uCluster);
}
else if (ProvidedObject is uint)
{
uint uAnotherCluster = (uint) ProvidedObject;
iResult = this.mFATEntry.uCluster.CompareTo(uAnotherCluster);
}
else if (ProvidedObject is string)
{
string sAnotherFileName = (string) ProvidedObject;
iResult = this.mFATEntry.sFileName.CompareTo(sAnotherFileName);
}
else
{
throw(new ArgumentException("CFATEntryChain.CompareTo - Exception,
compared parameter is not a CFATEntryChain"));
}
return iResult;
}
AND
public bool Equals(CFATEntryChain rhs)
{
if (rhs != null)
return this.CompareTo(rhs) == 0;
else
return false;
}
public override bool Equals(object obj)
{
if (obj != null)
{
if((obj is CFATEntryChain))
return this.Equals((CFATEntryChain) obj);
else
return false;
}
else
{
return false;
}
}
Cheers
Steve