Base keyword - HELP

  • Thread starter Thread starter Krishnan
  • Start date Start date
K

Krishnan

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
 
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
 
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
 
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.
 
Thanks for the reply. I missed out on mentioning the base class inheritance
in the derived class.
 
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.
 
Just one Q. The line,
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
 
Krishnan said:
Just one Q. The line,
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
 
Wait, so Dervied DOES or does NOT derive from class Base?

If it does, just use:

base.ImplMethod();
 
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
 
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 said:
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");
}
}

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
 
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
 
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 said:
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 said:
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>
 
Back
Top