473,382 Members | 1,392 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Base keyword - HELP

Hi,
Am having a base class that implements an interface (air code) :

class BaseClass : SomeInterface
{
void SomeInterface.ImplMethod()
{
// -- code here --
}
}

class Derived : SomeInterface
{
void SomeInterface.ImplMethod()
{
//NEED TO CALL BASE'S IMPLEMENTATION HERE
// -- code here --
}
}

Without making the implementations public, is there a way to do it?

TIA
Krishnan
Nov 17 '05 #1
17 2037
> class BaseClass : SomeInterface
class Derived : SomeInterface


The derived class doesn't inherit from the base class?

However, even when inheriting, I couldn't find a way to call the base
class interface function. Maybe someone else knows how to do it?

I only found this work-around, by moving the code to a helper function.
Like this:

interface SomeInterface
{
void ImplMethod();
}

class BaseClass : SomeInterface
{
protected void Method() { /* Code goes here */ }
void SomeInterface.ImplMethod() { Method() }
}

class Derived : BaseClass, SomeInterface
{
void SomeInterface.ImplMethod() { base.Method(); }
}

Greetings,
Wessel
Nov 17 '05 #2
>Without making the implementations public, is there a way to do it?

Your naming is a bit confusing since Derived doen't derive from
BaseClass. But you can do

SomeInterface si = new BaseClass();
si.ImplMethod();

if that's what you want.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #3
You could make the method internal if both classes are in the same assembly
or you really derive from that class and make the method protected.

"Krishnan" <kr*********************@myrealbox.com> schrieb im Newsbeitrag
news:eO**************@tk2msftngp13.phx.gbl...
Hi,
Am having a base class that implements an interface (air code) :

class BaseClass : SomeInterface
{
void SomeInterface.ImplMethod()
{
// -- code here --
}
}

class Derived : SomeInterface
{
void SomeInterface.ImplMethod()
{
//NEED TO CALL BASE'S IMPLEMENTATION HERE
// -- code here --
}
}

Without making the implementations public, is there a way to do it?

TIA
Krishnan

Nov 17 '05 #4
Thanks for the reply. I missed out on mentioning the base class inheritance
in the derived class.
"Wessel Troost" <no****@like.the.sun> wrote in message
news:op.styc180rxgtv7i@asbel...
class BaseClass : SomeInterface
class Derived : SomeInterface


The derived class doesn't inherit from the base class?

However, even when inheriting, I couldn't find a way to call the base
class interface function. Maybe someone else knows how to do it?

I only found this work-around, by moving the code to a helper function.
Like this:

interface SomeInterface
{
void ImplMethod();
}

class BaseClass : SomeInterface
{
protected void Method() { /* Code goes here */ }
void SomeInterface.ImplMethod() { Method() }
}

class Derived : BaseClass, SomeInterface
{
void SomeInterface.ImplMethod() { base.Method(); }
}

Greetings,
Wessel

Nov 17 '05 #5
Just one Q. The line,
void SomeInterface.ImplMethod() { Method() }

would call the most derived version's method, right? I mean there is no
"MyClass" keyword in C# :(

TIA
Krishnan
Krishnan "Pay your tax with a smile", says the government. I tried. They
wanted cash.
"Wessel Troost" <no****@like.the.sun> wrote in message
news:op.styc180rxgtv7i@asbel...
class BaseClass : SomeInterface
class Derived : SomeInterface


The derived class doesn't inherit from the base class?

However, even when inheriting, I couldn't find a way to call the base
class interface function. Maybe someone else knows how to do it?

I only found this work-around, by moving the code to a helper function.
Like this:

interface SomeInterface
{
void ImplMethod();
}

class BaseClass : SomeInterface
{
protected void Method() { /* Code goes here */ }
void SomeInterface.ImplMethod() { Method() }
}

class Derived : BaseClass, SomeInterface
{
void SomeInterface.ImplMethod() { base.Method(); }
}

Greetings,
Wessel

Nov 17 '05 #6
> class Derived : SomeInterface
{
void SomeInterface.ImplMethod()
{
//NEED TO CALL BASE'S IMPLEMENTATION HERE
base.ImplMethod();
// -- code here --
}
}

Nov 17 '05 #7
> Just one Q. The line,
void SomeInterface.ImplMethod() { Method() }

would call the most derived version's method, right? I mean there is no
"MyClass" keyword in C# :(

C# does have a "MyClass" keyword: this. You could write:

void SomeInterface.ImplMethod() { this.Method() }

Greetings,
Wessel
Nov 17 '05 #8
> base.ImplMethod();

That doesn't work. Try it.

Greetings,
Wessel
Nov 17 '05 #9

"Krishnan" <kr*********************@myrealbox.com> schrieb im Newsbeitrag
news:eM**************@tk2msftngp13.phx.gbl...
Just one Q. The line,
void SomeInterface.ImplMethod() { Method() }

would call the most derived version's method, right? I mean there is no
"MyClass" keyword in C# :(

Why this would be a problem?
In the code of Wessel Troost BaseClass.Method is not virtual, so there never
will be a more derived version.

Christof
Nov 17 '05 #10
Wait, so Dervied DOES or does NOT derive from class Base?

If it does, just use:

base.ImplMethod();

--
Adam Clauss

"Krishnan" <kr*********************@myrealbox.com> wrote in message
news:OR****************@TK2MSFTNGP15.phx.gbl...
Thanks for the reply. I missed out on mentioning the base class
inheritance in the derived class.
"Wessel Troost" <no****@like.the.sun> wrote in message
news:op.styc180rxgtv7i@asbel...
class BaseClass : SomeInterface
class Derived : SomeInterface


The derived class doesn't inherit from the base class?

However, even when inheriting, I couldn't find a way to call the base
class interface function. Maybe someone else knows how to do it?

I only found this work-around, by moving the code to a helper function.
Like this:

interface SomeInterface
{
void ImplMethod();
}

class BaseClass : SomeInterface
{
protected void Method() { /* Code goes here */ }
void SomeInterface.ImplMethod() { Method() }
}

class Derived : BaseClass, SomeInterface
{
void SomeInterface.ImplMethod() { base.Method(); }
}

Greetings,
Wessel


Nov 17 '05 #11
> Wait, so Dervied DOES or does NOT derive from class Base?
If it does, just use:

base.ImplMethod();

For some reason, that doesn't work if ImplMethod is an interface method.

Greetings,
Wessel
Nov 17 '05 #12
The following works just fine, just tried it (note - if Derived derives from
BaseClass, as mine does then ImplMethod must either be marked as new in
Derived, or virtual in base/override in Derived.):

public interface SomeInterface
{
void ImplMethod();
}
public class BaseClass : SomeInterface
{
public void ImplMethod()
{
Console.WriteLine("BaseClass.ImplMethod");
}
}
public class Derived : BaseClass
{
public new void ImplMethod()
{
base.ImplMethod();
Console.WriteLine("Derived.BaseClass");
}
}

--
Adam Clauss

"Wessel Troost" <no****@like.the.sun> wrote in message
news:op.styi8yiixgtv7i@asbel...
Wait, so Dervied DOES or does NOT derive from class Base?
If it does, just use:

base.ImplMethod();

For some reason, that doesn't work if ImplMethod is an interface method.

Greetings,
Wessel

Nov 17 '05 #13
"Adam Clauss" <ca*****@tamu.edu> schrieb im Newsbeitrag
news:11*************@corp.supernews.com...
The following works just fine, just tried it (note - if Derived derives
from BaseClass, as mine does then ImplMethod must either be marked as new
in Derived, or virtual in base/override in Derived.):

public interface SomeInterface
{
void ImplMethod();
}
public class BaseClass : SomeInterface
{
public void ImplMethod()
{
Console.WriteLine("BaseClass.ImplMethod");
}
}
public class Derived : BaseClass
{
public new void ImplMethod()
{
base.ImplMethod();
Console.WriteLine("Derived.BaseClass");
}
}

--
Adam Clauss


Yes, but that's implicit member implementation and the memeber is public.
The OP said it shall not be public, so he has to use explicit member
implementation.

Christof
Nov 17 '05 #14
> ImplMethod must either be marked as new in Derived, or virtual in
base/override in Derived.


Thanks, that was the information I was missing.

Greetings,
Wessel
Nov 17 '05 #15
Oh yes. You are right. I tried a similar workaround with the method marked
virtual and had a confusion. This should work fine (and actually, so should
my case ;) ).

Thanks a ton.

Krishnan

"Christof Nordiek" <cn@nospam.de> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...

"Krishnan" <kr*********************@myrealbox.com> schrieb im Newsbeitrag
news:eM**************@tk2msftngp13.phx.gbl...
Just one Q. The line,
void SomeInterface.ImplMethod() { Method() }

would call the most derived version's method, right? I mean there is no
"MyClass" keyword in C# :(

Why this would be a problem?
In the code of Wessel Troost BaseClass.Method is not virtual, so there
never will be a more derived version.

Christof

Nov 17 '05 #16
"Christof Nordiek" <cn@nospam.de> wrote in message
news:uf**************@TK2MSFTNGP12.phx.gbl...
Yes, but that's implicit member implementation and the memeber is public.
The OP said it shall not be public, so he has to use explicit member
implementation.


Huh? It's an interface method. It MUST be public. There is no other kind.

--
Adam Clauss
Nov 17 '05 #17
Adam Clauss <ca*****@tamu.edu> wrote:
"Christof Nordiek" <cn@nospam.de> wrote in message
news:uf**************@TK2MSFTNGP12.phx.gbl...
Yes, but that's implicit member implementation and the memeber is public.
The OP said it shall not be public, so he has to use explicit member
implementation.


Huh? It's an interface method. It MUST be public. There is no other kind.


No - if you use explicit interface implementation you end up with a
method which is sort of public and sort of private.

From the language spec:

<quote>
Explicit interface member implementations have different accessibility
characteristics than other members. Because explicit interface member
implementations are never accessible through their fully qualified name
in a method invocation or a property access, they are in a sense
private. However, since they can be accessed through an interface
instance, they are in a sense also public.
</quote>

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #18

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

Similar topics

10
by: Jacek Generowicz | last post by:
Where can I find concise, clear documentation describing what one has to do in order to enable Python's internal help to be able to provide descriptions of Python keywords ? I am in a situation...
1
by: Bob Rock | last post by:
Hello, I have read that the base keyword can be used not only to control a base class instantiation (thus calling one of the base class constructors) but also to access any other public or...
7
by: Rob Meade | last post by:
Hi all, Ok - I'm going around in circles and would appreciate some help. I have written a windows service, which up until I tried to be clever was working well :o) I have a main class which...
5
by: BartlebyScrivener | last post by:
Using ActivePython 2.4.3 on Windows XP While in the Pythonwin IDE, if I seek keyword help by issuing the following command: >>>help ('while') I get: Sorry, topic and keyword...
11
by: jyck91 | last post by:
// Base Conversion // Aim: This program is to convert an inputted number // from base M into base N. Display the converted // number in base N. #include <stdio.h> #include <stdlib.h>...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.