Re: need interface methods to be virtual-able

Tech-Archive recommends: Speed Up your PC by fixing your registry



class BaseClass : IFoo
{
public virtual void IFoo.Print()
{
Console.WriteLine("BaseClass::Print");
}
}
class DerivedClass : BaseClass
{
public override void IFoo.Print()
{
Console.WriteLine("DerivedClass::Print");
}
}

"Benny" <blee@xxxxxxxxxxxxxxxx> wrote in message
news:CFE60E69-711B-4358-9426-F450AC1F48DC@xxxxxxxxxxxxxxxx
Hi Greg,

interface IFoo
{
void Print();
}

class BaseClass : IFoo
{
void IFoo.Print()
{
Console.WriteLine("BaseClass::Print");
}
}

class DerivedClass : BaseClass
{
// CS0540 compliation error here. Trying to override Print method
void IFoo.Print()
{
Console.WriteLine("DerivedClass::Print");
}
}

static class Program
{
static void Main()
{
DerivedClass d = new DerivedClass();
BaseClass b = d;

b.Print(); // ideally, I want the Print defined in
DerivedClass
to be invoked
}
}

"Greg Young" wrote:

Still not following you .. try the following ..

[STAThread]
static void Main(string[] args)
{
A a = new A();
B b = new B();
b.CompareTo(a);
a.CompareTo(b);
IComparable foo = (IComparable) b;
foo.CompareTo(a);
}

Cheers,

Greg Young
MVP - C#

"Benny" <blee@xxxxxxxxxxxxxxxx> wrote in message
news:4BBA1A16-47EF-4FE9-A14E-016249D6FCB1@xxxxxxxxxxxxxxxx
Hi Greg, thanks for your reply.

The reason being is that polymorphasim will not work as expected. If I
have
a IComparable object, I would like to execute CompareTo based on the
derived
class and not on the base class.


"Greg Young" wrote:

I am not understanding why you cannot do this ...

class A : IComparable {
#region IComparable Members

public virtual int CompareTo(object obj) {
Console.WriteLine("In A");
return 0;
}

#endregion

}

class B : A {
public override int CompareTo(object obj) {
Console.WriteLine("In B");
return 0;
}
}

class Class1
{
[STAThread]
static void Main(string[] args)
{
A a = new A();
B b = new B();
b.CompareTo(a);
a.CompareTo(b);
}
}

output

in B
in A

Cheers,

Greg Young
MVP - C#

"Benny" <blee@xxxxxxxxxxxxxxxx> wrote in message
news:9BB7A331-97F5-4A52-B242-50AED5A2D0B5@xxxxxxxxxxxxxxxx
Thanks for the reply.

What I see is that the "CompareTo" coming from IComparable should be
virtual
so that if the following would work:

void foo()
{
DerivedClass d1 = new DerivedClass();
DerivedClass d2 = new DerivedClass();
...
ICompareable b1, b2;

...
b1 = d1;
b2 = d2;


b1.CompareTo(b2); // ideally, would like DerivedClass's
CompareTo
to be invoked
}

Thanks.


"Benny" wrote:

The methods within an interface is not virtual.

How do I go about doing the following:
class BaseClass : IComparable
{
int CompareTo(Object o) {...}
};

class DerivedClass : BasedClass
{
// compiler won't allow this since CompareTo is not virtual
int CompareTo(Object o) {...}
};


void foo()
{
DerivedClass d1 = new DerivedClass();
DerivedClass d2 = new DerivedClass();

...
b1 = d1;
b2 = d2;

BaseClass b1, b2;
b1.CompareTo(b2); // ideally, would like DerivedClass's
CompareTo
to be invoked
}


A solution I can think of is to write my own IComparable class and
make
the
functions virtual. Thanks.








.



Relevant Pages

  • Re: need interface methods to be virtual-able
    ... "Greg Young" wrote: ... class DerivedClass: BaseClass ... public override void IFoo.Print ...
    (microsoft.public.dotnet.general)
  • Re: need interface methods to be virtual-able
    ... void IFoo.Print ... class DerivedClass: BaseClass ... DerivedClass d = new DerivedClass; ... What I see is that the "CompareTo" coming from IComparable should be ...
    (microsoft.public.dotnet.general)
  • Re: Casting up to inheriting class from base?
    ... public void Foo() ... public class DerivedClass: BaseClass ... public override void Update() ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: need interface methods to be virtual-able
    ... public override int CompareTo{ ... static void Main ... What I see is that the "CompareTo" coming from IComparable should be ... DerivedClass d1 = new DerivedClass; ...
    (microsoft.public.dotnet.general)
  • Re: Protected Member
    ... | Then I had a class called CustomerList that was a List, ... Private visibility means that a member of a class can only ever be accessed ... class DerivedClass: BaseClass ...
    (microsoft.public.dotnet.general)