need interface methods to be virtual-able

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.
 
M

Mattias Sjögren

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
 
G

Guest

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.
 
G

Greg Young

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#
 
G

Guest

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.
 
G

Greg Young

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.
 
G

Guest

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.
 
G

Greg Young

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.
 
G

Guest

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.
 
J

Joanna Carter [TeamB]

"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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top