473,614 Members | 2,268 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Overriding interface members

Hi,

I'm confronted with a problem that seems not to be solvable. In
general: How can I override an interface member of my base class and
call the overridden method from my derived class?

This is my class:
class RemoteXmlDataSo urce : System.Web.UI.W ebControls.XmlD ataSource

And I want to override
DataSourceView System.Web.UI.I DataSource.GetV iew(string viewName)
but I want to make a call to the overridden method right after my
stuff.

Declaring the member with override, I get the message
The modifier 'override' is not valid for this item

When trying to access the "overridden " member with
(IDataSource)ba se).GetView(vie wName), I get the message
Use of keyword 'base' is not valid in this context

Ok I understand, my interface member completely replaces the member
from the base class. Is there any way to do this even though?
I tried something like
((IDataSource)( XmlDataSource)t his).GetView(vi ewName)
but that just calls my new member :)

That makes deriving much harder than just overriding a virtual method
and calling base.X()... But I hope I just did not fully understand ;)

Kind regards, Sebastian

Jun 21 '07 #1
10 6931
Hi,

"Sebastian" <se************ @googlemail.com wrote in message
news:11******** **************@ u2g2000hsc.goog legroups.com...
Hi,

I'm confronted with a problem that seems not to be solvable. In
general: How can I override an interface member of my base class and
call the overridden method from my derived class?

This is my class:
class RemoteXmlDataSo urce : System.Web.UI.W ebControls.XmlD ataSource

And I want to override
DataSourceView System.Web.UI.I DataSource.GetV iew(string viewName)
but I want to make a call to the overridden method right after my
stuff.

Declaring the member with override, I get the message
The modifier 'override' is not valid for this item
Did you declare the base as public virtual?

See if this code helps you:

class Program
{
static void Main(string[] args)
{
B b = new C();
b.M();

II i = b;
i.M();
Console.Read();
}
}

class B : II
{
public virtual void M() { Console.WriteLi ne("B.M"); }
}
class C : B
{
public override void M()
{
Console.WriteLi ne("C.M");
}
}

public interface II
{
void M();
}
Jun 21 '07 #2

"Ignacio Machin ( .NET/ C# MVP )" <machin TA laceupsolutions .comwrote in
message news:e2******** ******@TK2MSFTN GP04.phx.gbl...
Hi,

"Sebastian" <se************ @googlemail.com wrote in message
news:11******** **************@ u2g2000hsc.goog legroups.com...
>Hi,

I'm confronted with a problem that seems not to be solvable. In
general: How can I override an interface member of my base class and
call the overridden method from my derived class?

This is my class:
class RemoteXmlDataSo urce : System.Web.UI.W ebControls.XmlD ataSource

And I want to override
DataSourceVi ew System.Web.UI.I DataSource.GetV iew(string viewName)
but I want to make a call to the overridden method right after my
stuff.

Declaring the member with override, I get the message
The modifier 'override' is not valid for this item

Did you declare the base as public virtual?

See if this code helps you:

class Program
{
static void Main(string[] args)
{
B b = new C();
b.M();

II i = b;
i.M();
Console.Read();
}
}

class B : II
{
public virtual void M() { Console.WriteLi ne("B.M"); }
I assume that the base class method implements an interface, but doesn't use
the virtual keyword (that might make it sealed).
}
class C : B
{
public override void M()
{
Console.WriteLi ne("C.M");
}
}

public interface II
{
void M();
}
Jun 22 '07 #3
It turns out that declaring explicit interface implementations as
virtual is not possible - only implicit interface implementation
support that. Seems to be a limitation of the C# language. Or is there
a way I do not see?

Look at this snippet:

interface IFace {
string iFaceMethod();
}

public class BaseClass : IFace {
// works (implicit interface implementation)
public virtual string iFaceMethod() { return "BaseClass public
virtual string iFaceMethod()"; }

// works (explicit interface implementation - obove method won't be
anymore an implicit interface implementation)
string IFace.iFaceMeth od() { return "BaseClass string
IFace.iFaceMeth od()"; }

// The modifier 'public' is not valid for this item
// The modifier 'virtual' is not valid for this item
//public virtual string IFace.iFaceMeth od() { }
}

public class DerivedClass : BaseClass, IFace {

// works
public override string iFaceMethod() {
// Use of keyword 'base' is not valid in this context
//return ((IFace)base).i FaceMethod();

// works
//return base.iFaceMetho d(); // calls BaseClass public virtual
string iFaceMethod()

return "DerivedCla ss public override string iFaceMethod()";
}

// works, but would definitely hide base.iFaceMetho d
string IFace.iFaceMeth od() {
// Use of keyword 'base' is not valid in this context
//return ((IFace)base).i FaceMethod();

// works
//return base.iFaceMetho d(); // calls BaseClass public virtual
string iFaceMethod()

// works, but leads to infinite recursion
//return ((IFace)this).i FaceMethod();

return "DerivedCla ss string IFace.iFaceMeth od()";
}

// The modifier 'public' is not valid for this item
// The modifier 'override' is not valid for this item
//public override string IFace.iFaceMeth od() { }
}

Kind regards, Sebastian

Jun 27 '07 #4

"Sebastian" <se************ @googlemail.com wrote in message
news:11******** **************@ c77g2000hse.goo glegroups.com.. .
It turns out that declaring explicit interface implementations as
virtual is not possible - only implicit interface implementation
support that. Seems to be a limitation of the C# language. Or is there
a way I do not see?
No, you're right. explicit interface implementations are always private
(therefore can't be virtual), and implicit implementations are always
public. There's no way to get anything in-between.
>
Look at this snippet:

interface IFace {
string iFaceMethod();
}

public class BaseClass : IFace {
// works (implicit interface implementation)
public virtual string iFaceMethod() { return "BaseClass public
virtual string iFaceMethod()"; }

// works (explicit interface implementation - obove method won't be
anymore an implicit interface implementation)
string IFace.iFaceMeth od() { return "BaseClass string
IFace.iFaceMeth od()"; }

// The modifier 'public' is not valid for this item
// The modifier 'virtual' is not valid for this item
//public virtual string IFace.iFaceMeth od() { }
}

public class DerivedClass : BaseClass, IFace {

// works
public override string iFaceMethod() {
// Use of keyword 'base' is not valid in this context
//return ((IFace)base).i FaceMethod();

// works
//return base.iFaceMetho d(); // calls BaseClass public virtual
string iFaceMethod()

return "DerivedCla ss public override string iFaceMethod()";
}

// works, but would definitely hide base.iFaceMetho d
string IFace.iFaceMeth od() {
// Use of keyword 'base' is not valid in this context
//return ((IFace)base).i FaceMethod();

// works
//return base.iFaceMetho d(); // calls BaseClass public virtual
string iFaceMethod()

// works, but leads to infinite recursion
//return ((IFace)this).i FaceMethod();

return "DerivedCla ss string IFace.iFaceMeth od()";
}

// The modifier 'public' is not valid for this item
// The modifier 'override' is not valid for this item
//public override string IFace.iFaceMeth od() { }
}

Kind regards, Sebastian

Jun 27 '07 #5
No, you're right. explicit interface implementations are always private
(therefore can't be virtual), and implicit implementations are always
public. There's no way to get anything in-between.
Why are implicit implementations always public and explicit interface
implementations are always private?
Is there any logical explanation for that? For me, it sounds just
stupid. When implementing two methods from different interfaces that
have the same name, the methods can never be overridden because they
have to be explicit.

Kind regards, Sebastian

Jul 4 '07 #6
On Jul 4, 3:19 pm, Sebastian <sebpaul79.w... @googlemail.com wrote:
No, you're right. explicit interface implementations are always private
(therefore can't be virtual), and implicit implementations are always
public. There's no way to get anything in-between.

Why are implicit implementations always public and explicit interface
implementations are always private?
Is there any logical explanation for that? For me, it sounds just
stupid. When implementing two methods from different interfaces that
have the same name, the methods can never be overridden because they
have to be explicit.
I think I've got a solution, actually. You just need to reimplement
the interface. You don't specify that you're overriding it, just that
you're implementing it. Here's some code to demonstrate what I mean -
if it doesn't do what you want it to, it would be good to know exactly
what's still missing. Note that you have to redeclare that you're
implementing IDisposable in the derived class.

using System;

public class Base : IDisposable
{
void IDisposable.Dis pose()
{
Console.WriteLi ne ("Base");
}
}

public class Derived : Base, IDisposable
{
void IDisposable.Dis pose()
{
Console.WriteLi ne ("Derived");
}
}

public class Test
{
static void Main()
{
IDisposable a = new Base();
IDisposable b = new Derived();

a.Dispose();
b.Dispose();
}
}

Jon

Jul 4 '07 #7
This is a feature of the C# compiler (I believe you have other options
in VB for instance). You can, however, simply write a protected inner
method for each, and forward from the explicit interface call - i.e.

void ISomeInterface. SomeMethod() {SomeInterfaceS omeMethod();}
void IOtherInterface .SomeMethod() {OtherInterface SomeMethod();}
protected virtual void SomeInterfaceSo meMethod() {...}
protected virtual void OtherInterfaceS omeMethod() {...}

Marc
Jul 4 '07 #8
On Jul 4, 4:33 pm, "Jon Skeet [C# MVP]" <s...@pobox.com wrote:
I think I've got a solution, actually. You just need to reimplement
the interface. You don't specify that you're overriding it, just that
you're implementing it.
In that case, you cannot call the overridden method, as it would be
possible in virtual methods (base keyword). I tried to show this in my
last code example - maybe it's hard legible because of the line
wraps...

On Jul 4, 4:37 pm, "Marc Gravell" <marc.grav...@g mail.comwrote:
This is a feature of the C# compiler
LOL In my oppinion it's a defect :D
simply write a protected inner
method for each, and forward from the explicit interface call
Yes this would work. But this is not possible with existing (i.e.
built in .NET) classes that do not implement that way...

Sebastian

Jul 4 '07 #9
On Jul 4, 3:56 pm, Sebastian <sebpaul79.w... @googlemail.com wrote:
I think I've got a solution, actually. You just need to reimplement
the interface. You don't specify that you're overriding it, just that
you're implementing it.

In that case, you cannot call the overridden method, as it would be
possible in virtual methods (base keyword). I tried to show this in my
last code example - maybe it's hard legible because of the line
wraps...
That's true. It can be done with reflection, but I agree that's pretty
grim:

void IDisposable.Dis pose()
{
Console.WriteLi ne ("Derived");
InterfaceMappin g mapping =
typeof(Base).Ge tInterfaceMap(t ypeof(IDisposab le));
MethodInfo method = mapping.TargetM ethods[0];
method.Invoke(t his, null);
}

(I only used 0 blindly because IDisposable has only one member.)

Jon

Jul 4 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
4634
by: Anon Email | last post by:
Hi people, I'm learning about header files in C++. The following is code from Bartosz Milewski: // Code const int maxStack = 16; class IStack
14
4818
by: Laurent Vigne | last post by:
Hello, I would like to know how to make the methods of an interface inaccessible from outside Example: ---------------------------------------------- internal Interface ImyInterface { void MyPrivateMethod(); // I cant define any accessibility here, well ok ! }
21
13815
by: Helge Jensen | last post by:
I've got some data that has Set structure, that is membership, insert and delete is fast (O(1), hashing). I can't find a System.Collections interface that matches the operations naturally offered by Sets. - ICollection cannot decide containment - IList promises indexability by the natural numbers, which is not achievable (since i hash elements, not sort them). - IDictionary is definatly not setlike. Although I can, of course, define...
1
3696
by: Joel | last post by:
Why does this work: using System; namespace ConsoleApplication1 { class Class1 { static void Main(string args)
2
2018
by: byoukstetter | last post by:
So, I have an interface with several overriding methods: using System; using System.Collections.Specialized; namespace some.name.space { public interface IVrsPersistenceProvider { string Select(string idName, string tableName, string whereClause);
4
5950
by: Phill. W | last post by:
Here's a knotty little problem. I have some nasty little controls that needs to behave in a non- windows-Standard way - don't ask why; it's a large application being converted from some older code and my users are adamant that this behaviour mustn't change. Specifically, I want to be able to set myControl.Enabled = False
13
5155
by: ctor | last post by:
Hi, I'm experiencing an annoying issue. Here is a simplified idea of what I am trying to do. Inclusion guards aren't shown for readability. I hope this isn't too confusing; I don't think a description would be as clear as an example. ----------- class ParentA {};
8
1472
by: khalprin | last post by:
Hello, I'm trying to create a component that will be used from .net clients and COM clients. I've got an object model that looks something like this: ISystem IRuntime IConfiguration ICollectionOfConfigurableThings IConfigurableThing
52
20859
by: Ben Voigt [C++ MVP] | last post by:
I get C:\Programming\LTM\devtools\UselessJunkForDissassembly\Class1.cs(360,27): error CS0535: 'UselessJunkForDissassembly.InvocableInternals' does not implement interface member 'UselessJunkForDissassembly.IInvocableInternals.OperationValidate(string)' C:\Programming\LTM\devtools\UselessJunkForDissassembly\Class1.cs(360,27): error CS0535: 'UselessJunkForDissassembly.InvocableInternals' does not implement interface member...
0
8180
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8623
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8576
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8275
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7091
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5538
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2566
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1423
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.