I just switched from Java and as most already know Java incorporates a powerfully generic tool called Comparable. It seems as if C# has this same tool but in all the examples I have found... one way or another, I have to hard code a cast type to compare to... is this the only solution?
Main BST code not implemented
Tree - main driver class that controls all dictionary operations of the tree
TreeNode - of type IComparable, has a Node, left, and a Right (pretty standard)
Node - of type IComparable with a private IComparable object as its data
public class Node: IComparable
{
private IComparable obj;
public IComparable Obj
{
get
{ return (obj); }
set
{ obj = value; }
}
public Node()
{
obj = null;
}
public Node(IComparable integer)
{
obj = integer;
}
public int CompareTo(object obj)
{
if (obj is IComparable)
return ( (this.obj).CompareTo( (IComparable)obj ) );
else
throw new Exception("Your object is not of type IComparable!");
}
new public bool Equals(object obj)
{ return( this.obj.Equals(obj) ); }
public override string ToString()
{ return( this.obj.ToString() ); }
}
I always come to a debugging error in VS stating that my IComparable must be of type (whatever I am using at that moment for data) in this particular case an Int32 type.
Can this code be generalized? More specifically, can this code be set up that it never needs to know the cast type?
Main BST code not implemented
Tree - main driver class that controls all dictionary operations of the tree
TreeNode - of type IComparable, has a Node, left, and a Right (pretty standard)
Node - of type IComparable with a private IComparable object as its data
public class Node: IComparable
{
private IComparable obj;
public IComparable Obj
{
get
{ return (obj); }
set
{ obj = value; }
}
public Node()
{
obj = null;
}
public Node(IComparable integer)
{
obj = integer;
}
public int CompareTo(object obj)
{
if (obj is IComparable)
return ( (this.obj).CompareTo( (IComparable)obj ) );
else
throw new Exception("Your object is not of type IComparable!");
}
new public bool Equals(object obj)
{ return( this.obj.Equals(obj) ); }
public override string ToString()
{ return( this.obj.ToString() ); }
}
I always come to a debugging error in VS stating that my IComparable must be of type (whatever I am using at that moment for data) in this particular case an Int32 type.
Can this code be generalized? More specifically, can this code be set up that it never needs to know the cast type?