bpeikes

Lets say you have two interfaces:
interface IA: IComparable
{
int A {get; set;}
}

interface IB: IComparable
{
int B {get; set;}
}

and a class which implements both IA and IB
class C: IA, IB
{

}

class C now has to implement CompareTo, but the problem is that CompareTo should do different things if being used to compare this object to another IA, IB or classC. How do you implement the CompareTo function For instance lets say there is code like this:
C c1 = new C();
C c2 = new C();

IA ia = c2;
int comp1 = c1.CompareTo(ia);
int comp2 = c1.CompareTo(c1);

In this case, comp1 should equal 0 if c1.A == c2.A, and comp2 should equal 0 if both c1.A and c1.B are equal to c2.A and c2.B.

Since you can only have one CompareTo method on class C how do you check if the interface was passed in or the object was passed in



Re: Visual C# Language Multi Interface inheritance

Mark Dawson

Hi,

you can use explicit interface declaration rather than implicit to determine which method gets called i.e.

class C : IA, IB

{

public int IA.CompareTo(.....)

{

}

public int IB.CompareTo(.....)

{

}

}

C c1 = new C1();

int comp1 = ((IA)c1).CompareTo(.....)

Mark.






Re: Visual C# Language Multi Interface inheritance

BlueMikey

Here is the short answer: you can't.

You always pass an object in to a method, the interface is just one way of representing it. So whether or not your code references c2 as if it was an IA or as if it were a C, it doesn't matter, the object is a C.

I think the problem is an error in your design. Since C always implements IA and IB, when you compare two Cs, you should always want to look at both IA and IB.

Why not have an A object and a B object, and then C could maybe store references to an A or a B, and the CompareTo in C would check to see if a B exists. It seems silly to force C to not only implement a property, but implement the storage means for that property too.




Re: Visual C# Language Multi Interface inheritance

micvos

CompareTo is used to compare to objects of the same the type. In this case there's only one place to implement it and that is in the class C. In the method you must not compare it with an interface but with an instance of th C class:

public int CompareTo(object obj) {

if(obj is C)

//Do comparison here and return value

else

throw new ArgumentException("object is not of type C");

}






Re: Visual C# Language Multi Interface inheritance

micvos

Mark,

I don't think this is possible because the CompareTo() method is declared in the Comparable interface, not in IA or IB. So there can be only one single implementation of CompareTo(). Haven't tried it but im pretty sure this won't compile.






Re: Visual C# Language Multi Interface inheritance

bpeikes

The problem is actually that the library with the interfaces was written in 1.1. If they were in 2.0, they would have been:

interface IA: IComparable<IA>

Then the CompareTo function is very clear. Since I don't have access to the libraries I have a way around it but I need to know the best way to check if an object implements a particular interface....

I figure I could do something like this:

int CompareTo(object obj)
{
bool implementsIA, implementsIB = true;
// I know the following code doesn't work. I'm looking for a recommendation on what to use for the "implements" function
if(obj implements IA && obj implements IB) {
// compare as IA and IB
}
if(obj implements IA) {
// Code to compare this as an IA
}
if(obj implents IB) {
// Code to compare this as an IB
}
}

What's the best way to check for an interface





Re: Visual C# Language Multi Interface inheritance

micvos

you can use the keyword 'is'. This will work, but it's not a good design and use of the CompareTo() method.




Re: Visual C# Language Multi Interface inheritance

BlueMikey

No, that won't work.

If you have C implement IA and IB and you pass an instance of C into a method that takes an Object, the Is keyword will return True for checking against C, IA, and IB.

He wants to know what the type of the variable representing C was before C got passed to the method, which is absolutely impossible to know.




Re: Visual C# Language Multi Interface inheritance

micvos

That's not completly impossible. You can use method overloading, but you have be very explicit when you call the CompareTo() function:

public int CompareTo(object obj)

{

//dosomething

}

public int CompareTo(IA obj)

{

//dosomething

}

public int CompareTo(IB obj)

{

//dosomething

}

Now you can us a call like instanceOfC.CompareTo((IA)anotherInstanceOfC);






Re: Visual C# Language Multi Interface inheritance

BlueMikey

Which pretty much defeats the entire purpose of what he wants to do, not to mention that you still can't guarantee that CompareTo will work as desired when using things like built-in sort methods.

So, I guess it's not impossible, but for the use that the first post stated, it is.




Re: Visual C# Language Multi Interface inheritance

bpeikes

Actually, I have a better way of doing it:

int CompareTo(object obj)
{
IA ia = obj as IA;
IB ib = obj as IB;
if(ia != null && ib != null)
{
// Compare both interfaces
}
else if (ia != null && ib == null)
{
// Compare just the IA parts
}
else if(ia == null && ib != null)
{
// Compare the IB parts
}

}





Re: Visual C# Language Multi Interface inheritance

Mark Dawson

You are completely right, this was the first post of the morning - need coffee first before posting :-)

Mark.






Re: Visual C# Language Multi Interface inheritance

BlueMikey

But you'll always hit the first case on that one. obj is, explicitly, able to be defined as both IA and IB.




Re: Visual C# Language Multi Interface inheritance

micvos

bpeikes,

You're joking, right






Re: Visual C# Language Multi Interface inheritance

bpeikes

Joking about what

This works for code in this situation. Add a class D which only implements IB.

C c = new C();

D d = new D();

// This checks the the IB interfaces
if(c.CompareTo(d) == 0) {

}