Re: need interface methods to be virtual-able
- From: "Greg Young" <DruckDruckGoose@xxxxxxxxxxx>
- Date: Tue, 9 May 2006 17:34:40 -0400
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.
.
- Follow-Ups:
- Re: need interface methods to be virtual-able
- From: Benny
- Re: need interface methods to be virtual-able
- References:
- Re: need interface methods to be virtual-able
- From: Greg Young
- Re: need interface methods to be virtual-able
- From: Benny
- Re: need interface methods to be virtual-able
- From: Greg Young
- Re: need interface methods to be virtual-able
- From: Benny
- Re: need interface methods to be virtual-able
- Prev by Date: Re: need interface methods to be virtual-able
- Next by Date: Re: A legal question about obfuscation.
- Previous by thread: Re: need interface methods to be virtual-able
- Next by thread: Re: need interface methods to be virtual-able
- Index(es):
Relevant Pages
|