Hi everyone!
First of all I want to apologize if this is some kind of repost. Unfortunately I don't even know what keywords to search. I have something like this:
abstract class BaseValueClass { ... }
class IntegerValue : BaseValueClass {
public int CompareTo(IntegerValue integerValue) {...}
}
class StringValue : BaseValueClass {
public int CompareTo(StringValue stringValue) {...}
}
It's a dumb example, I know. But it is a simple way to explain what I want do achieve. Now, I have a collection:
List
I want to iterate all the items in valueList and I want to call the CompareTo method. How can I achieve this? How can I declare the CompareTo method in the BaseValueClass and override it in the derived classes, knowing that this method will have diferrent parameters in each one of the derived classes?
Thanks!
Karthik JegadeesanPosted Jan 19, 2009, 4:19 PM
Carl SchraderPosted Jan 18, 2009, 11:43 AM
Ricardo,
Maybe you should take a look into the IComparable interface? This interface contains the CompareTo method with the following signature:
int CompareTo(object obj);
What you can do is have your base class implement the interface and have the derived classes flesh out the details of the method:
class Program
{
static void Main(string[] args)
{
}
}
abstract class BaseValueClass : IComparable
{
// Nothing right now
}
class IntegerValue : BaseValueClass
{
public int CompareTo(object integerValue)
{
if (integerValue is IntegerValue)
{
// Do operation
}
}
}
class StringValue : BaseValueClass
{
public int CompareTo(object stringValue)
{
if (stringValue is StringValue)
{
// Do operation
}
}
}
Carl