need interface methods to be virtual-able

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.
 
wrote:
The methods within an interface is not virtual.

What matters is that the method implementation (in BaseClass in your
example) is virtual. And you can make that virtual the same way as any
other method.


Mattias
 
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.
 
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#
 
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 said:
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 said:
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.
 
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 said:
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 said:
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 said:
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.


:

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.
 
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 said:
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 said:
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 said:
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#

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.


:

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.
 
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 said:
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 said:
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 said:
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.


:

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#

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.


:

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.
 
Thanks for your excellent help!


Greg Young said:
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 said:
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 said:
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#

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.


:

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#

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.


:

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.
 
"Greg Young" <[email protected]> a écrit dans le message de (e-mail address removed)...

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

This is not allowed; it simply won't compile. You have to do the following :

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

void IFoo.Print()
{
Print();
}
}

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

Joanna
 
Back
Top