By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,952 Members | 1,073 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,952 IT Pros & Developers. It's quick & easy.

Calling explicity interface implementation from a subclass

P: n/a
Could someone tell me if it's possible (and if so, how) to call an
explicitly-implemented interface method from a subclass? I have a class in
which I have to explicity implement some methods, but my subclasses, which
should use them, cannot call them. I'm not sure if it just isn't possible
due to visibility or if I just don't have the syntax right. I can't make
the methods protected, as the compiler complains (although the documentation
for the error only says it's true of the "public" accessor, "protected" also
doesn't work, but when they use the default accessor, I can't get the
compiler to see the methods.
Nov 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Keith,

I am curious, since it is an interface implementation, why not just cast
the "this" pointer to the interface and make the call? Or do you mean you
want to call the base implementation?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Keith Patrick" <ri*******************@nospamhotmail.com> wrote in message
news:eU****************@TK2MSFTNGP12.phx.gbl...
Could someone tell me if it's possible (and if so, how) to call an
explicitly-implemented interface method from a subclass? I have a class in which I have to explicity implement some methods, but my subclasses, which
should use them, cannot call them. I'm not sure if it just isn't possible
due to visibility or if I just don't have the syntax right. I can't make
the methods protected, as the compiler complains (although the documentation for the error only says it's true of the "public" accessor, "protected" also doesn't work, but when they use the default accessor, I can't get the
compiler to see the methods.

Nov 16 '05 #2

P: n/a
Hmmm...I tried this morning, but couldn't get it to work, but now it *is*
compiling (I hadn't dosed up with caffeine yet, so I could have been missing
something obvious). One major issue I am having that could have played a
role is that due to my hierarchy, I am having to do some delayed iface
implementations in subclasses (IDictionary.GetEnumerator vs.
IEnumerable.GetEnumerator is the primary culprit). Because I don't
implement ICollection at the base level, I am doing a lot of weird
implementations of my various methods. I'm going to rewrite my base classes
from scratch to get a cleaner look at my methods, and if things still aren't
working, I'll post actual code (right now, my code is too verbose to really
be postable within the context of this thread).

Thanks for the help!
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uY**************@tk2msftngp13.phx.gbl...
Keith,

I am curious, since it is an interface implementation, why not just cast the "this" pointer to the interface and make the call? Or do you mean you
want to call the base implementation?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Keith Patrick" <ri*******************@nospamhotmail.com> wrote in message
news:eU****************@TK2MSFTNGP12.phx.gbl...
Could someone tell me if it's possible (and if so, how) to call an
explicitly-implemented interface method from a subclass? I have a class

in
which I have to explicity implement some methods, but my subclasses, which should use them, cannot call them. I'm not sure if it just isn't possible due to visibility or if I just don't have the syntax right. I can't make the methods protected, as the compiler complains (although the

documentation
for the error only says it's true of the "public" accessor, "protected"

also
doesn't work, but when they use the default accessor, I can't get the
compiler to see the methods.


Nov 16 '05 #3

P: n/a
Here's a problem I'd forgotten about that further complicates things: My
hierarchy is like so:
abstract class EventfulCollection: ICollection
abstract class EventfulListCollection: IList (note to MS: FXCop flags
"EventfulLIst" as wrong because it implements ICollection and should
therefore end in "Collection". Why does FXCop not realize that it actually
implements the subinterface IList and let me name the thing IList? .Net
itself breaks this rule with ArrayList!)

The problem this causes is that I must declare ICollection.CopyTo(...), but
I don't want to implement the methods here, allowing my subclasses (some are
ILists, others are IDictionaries) to implement it themselves (and it isn't
just this method or this interface). So I can't say:
protected abstract CopyTo(...);

but I don't want to say:
ICollection.CopyTo(...) {...} because then I'm implementing code that I
don't want to implement here (EventfulCollection has no private collection,
deferring that until the subclasses, so I have to implement it as a no-op,
which I HATE doing in OO). I want EventfulCollection to satisfy ICollection
responsibilities at an absolute minimum so that actual implementation occurs
in the subclasses. However, I still need to hide the implementations such
that my type-specific collections have their own implementations that are
overridden versions of the parent methods.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uY**************@tk2msftngp13.phx.gbl...
Keith,

I am curious, since it is an interface implementation, why not just cast the "this" pointer to the interface and make the call? Or do you mean you
want to call the base implementation?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Keith Patrick" <ri*******************@nospamhotmail.com> wrote in message
news:eU****************@TK2MSFTNGP12.phx.gbl...
Could someone tell me if it's possible (and if so, how) to call an
explicitly-implemented interface method from a subclass? I have a class

in
which I have to explicity implement some methods, but my subclasses, which should use them, cannot call them. I'm not sure if it just isn't possible due to visibility or if I just don't have the syntax right. I can't make the methods protected, as the compiler complains (although the

documentation
for the error only says it's true of the "public" accessor, "protected"

also
doesn't work, but when they use the default accessor, I can't get the
compiler to see the methods.


Nov 16 '05 #4

P: n/a
Keith,

I've had this problem as well. I think that the best way to get around
it (although the most tedious) is to have abstract methods on the base class
which the implementation of the interface calls. Then, your sub classes
would just override the abstract methods, and you shouldn't have a problem
then.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Keith Patrick" <ri*******************@nospamhotmail.com> wrote in message
news:eS**************@TK2MSFTNGP12.phx.gbl...
Here's a problem I'd forgotten about that further complicates things: My
hierarchy is like so:
abstract class EventfulCollection: ICollection
abstract class EventfulListCollection: IList (note to MS: FXCop flags
"EventfulLIst" as wrong because it implements ICollection and should
therefore end in "Collection". Why does FXCop not realize that it actually implements the subinterface IList and let me name the thing IList? .Net
itself breaks this rule with ArrayList!)

The problem this causes is that I must declare ICollection.CopyTo(...), but I don't want to implement the methods here, allowing my subclasses (some are ILists, others are IDictionaries) to implement it themselves (and it isn't
just this method or this interface). So I can't say:
protected abstract CopyTo(...);

but I don't want to say:
ICollection.CopyTo(...) {...} because then I'm implementing code that I
don't want to implement here (EventfulCollection has no private collection, deferring that until the subclasses, so I have to implement it as a no-op,
which I HATE doing in OO). I want EventfulCollection to satisfy ICollection responsibilities at an absolute minimum so that actual implementation occurs in the subclasses. However, I still need to hide the implementations such
that my type-specific collections have their own implementations that are
overridden versions of the parent methods.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:uY**************@tk2msftngp13.phx.gbl...
Keith,

I am curious, since it is an interface implementation, why not just

cast
the "this" pointer to the interface and make the call? Or do you mean you
want to call the base implementation?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Keith Patrick" <ri*******************@nospamhotmail.com> wrote in message news:eU****************@TK2MSFTNGP12.phx.gbl...
Could someone tell me if it's possible (and if so, how) to call an
explicitly-implemented interface method from a subclass? I have a
class in
which I have to explicity implement some methods, but my subclasses, which should use them, cannot call them. I'm not sure if it just isn't possible due to visibility or if I just don't have the syntax right. I can't make the methods protected, as the compiler complains (although the

documentation
for the error only says it's true of the "public" accessor,

"protected" also
doesn't work, but when they use the default accessor, I can't get the
compiler to see the methods.



Nov 16 '05 #5

P: n/a
The difficulty there (and I don't know if this is really the fault of the
language or the design of the hierarchy) is that I can't expose
IEnumerable.GetEnumerator() because my dictionary-based implementation has
to expose it as IDictionary.GetEnumerator(), which has a different return
type, so the dictionary version cannot expose both methods (unless one is an
explicity definition, which means I have to implement what should be
abstract)
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:OR*************@TK2MSFTNGP10.phx.gbl...
Keith,

I've had this problem as well. I think that the best way to get around it (although the most tedious) is to have abstract methods on the base class which the implementation of the interface calls. Then, your sub classes
would just override the abstract methods, and you shouldn't have a problem
then.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Keith Patrick" <ri*******************@nospamhotmail.com> wrote in message
news:eS**************@TK2MSFTNGP12.phx.gbl...
Here's a problem I'd forgotten about that further complicates things: My
hierarchy is like so:
abstract class EventfulCollection: ICollection
abstract class EventfulListCollection: IList (note to MS: FXCop flags
"EventfulLIst" as wrong because it implements ICollection and should
therefore end in "Collection". Why does FXCop not realize that it

actually
implements the subinterface IList and let me name the thing IList? .Net
itself breaks this rule with ArrayList!)

The problem this causes is that I must declare ICollection.CopyTo(...),

but
I don't want to implement the methods here, allowing my subclasses (some

are
ILists, others are IDictionaries) to implement it themselves (and it isn't just this method or this interface). So I can't say:
protected abstract CopyTo(...);

but I don't want to say:
ICollection.CopyTo(...) {...} because then I'm implementing code that I
don't want to implement here (EventfulCollection has no private

collection,
deferring that until the subclasses, so I have to implement it as a no-op, which I HATE doing in OO). I want EventfulCollection to satisfy

ICollection
responsibilities at an absolute minimum so that actual implementation

occurs
in the subclasses. However, I still need to hide the implementations such that my type-specific collections have their own implementations that are overridden versions of the parent methods.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote

in
message news:uY**************@tk2msftngp13.phx.gbl...
Keith,

I am curious, since it is an interface implementation, why not just
cast
the "this" pointer to the interface and make the call? Or do you mean

you want to call the base implementation?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Keith Patrick" <ri*******************@nospamhotmail.com> wrote in message news:eU****************@TK2MSFTNGP12.phx.gbl...
> Could someone tell me if it's possible (and if so, how) to call an
> explicitly-implemented interface method from a subclass? I have a class in
> which I have to explicity implement some methods, but my subclasses,

which
> should use them, cannot call them. I'm not sure if it just isn't

possible
> due to visibility or if I just don't have the syntax right. I can't

make
> the methods protected, as the compiler complains (although the
documentation
> for the error only says it's true of the "public" accessor, "protected" also
> doesn't work, but when they use the default accessor, I can't get

the > compiler to see the methods.
>
>



Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.