PC Review


Reply
Thread Tools Rate Thread

Deferring implementation of interface to inner class

 
 
Arjan de Haan
Guest
Posts: n/a
 
      7th May 2009
Hi.

I'm at a loss here. I seem to remember that it is possible to allow a
nested class to implement an interface that is declared on the outer
class, like this:

public class Outer : IMyInterfave
{
private readonly Inner _innerImpl = new Inner();

private class Inner : IMyInterface
{
public int FirstMethod()
{
...
}

public void SecondMethod()
{
...
}
}

public void OtherMethod()
{
...
}

//
// The next 2 methods I DONT want
//
public int FirstMethod()
{
return _innerImpl.FirstMethod();
}

public void SecondMethod()
{
return _innerImpl.SecondMethod();
}

}

I know I can get the behavior I want if I implement FirstMethod() and
SecondMethod() in Outer and call the same methods on the instance of the
inner class (see above).
But, would this be possible with help from the compiler? In other words,
would it be possible to tell (using Attribites?) to tell the compiler
that the interface IMyInterface for class Outer is implemented using
Inner? If so, that would mean the methods for the interface as declared
in Outer would not be necessary. Perhaps by introducing a property of
the Interface and adding a Attribute to it, like so:

public class Outer : IMyInterface
{
private readonly Inner _innerImpl = new Inner();

private class Inner : IMyInterface
{
...
...
}

public void OtherMethod()
{
...
}

[Implements(IMyInterface)]
public Inner InnerClass { get { return _innerImpl; } }
}

Now I don't how to write the boring IMyInterface methods and delegation
to the inner class.

Is this possible using C#?

Tx.
....Arjan...
 
Reply With Quote
 
 
 
 
Peter Morris
Guest
Posts: n/a
 
      7th May 2009
It was in Delphi for example but not in C#. In fact even Delphi for .NET
removed this feature so I suspect it isn't possible in .NET at all, the
compile would have to implement those methods automatically and pass them
on.



--
Pete
====
http://mrpmorris.blogspot.com
http://www.AlterEgos.com - Who do you want to be?

 
Reply With Quote
 
Arjan de Haan
Guest
Posts: n/a
 
      7th May 2009
Peter Morris wrote:
> It was in Delphi for example but not in C#. In fact even Delphi for
> .NET removed this feature so I suspect it isn't possible in .NET at all,
> the compile would have to implement those methods automatically and pass
> them on.
>
>
>


Yes. I'm coming from a Delphi background :-D
I thought it was a handy feature, and since Anders Hejlsberg designed
(most of) C# too, I was hoping this feature got in somehow.

....Arjan...
 
Reply With Quote
 
Ignacio Machin ( .NET/ C# MVP )
Guest
Posts: n/a
 
      7th May 2009
On May 7, 6:44*am, Arjan de Haan
<adwhaan.remove_t...@raketnet.remove_this_too.nl> wrote:
> Hi.
>
> I'm at a loss here. I seem to remember that it is possible to allow a
> nested class to implement an interface that is declared on the outer
> class, like this:


Hi,

No it's not , and IIRC neither in C++ or in any other language I
remember , The nested class is simply another type that is declared
like that to prevent naming conflict (and also to signally the
dependence between the types)
 
Reply With Quote
 
Arjan de Haan
Guest
Posts: n/a
 
      8th May 2009
Ignacio Machin ( .NET/ C# MVP ) wrote:
> On May 7, 6:44 am, Arjan de Haan
> <adwhaan.remove_t...@raketnet.remove_this_too.nl> wrote:
>> Hi.
>>
>> I'm at a loss here. I seem to remember that it is possible to allow a
>> nested class to implement an interface that is declared on the outer
>> class, like this:

>
> Hi,
>
> No it's not , and IIRC neither in C++ or in any other language I
> remember , The nested class is simply another type that is declared
> like that to prevent naming conflict (and also to signally the
> dependence between the types)


Ignacio,

thanks for your reply. I didn't remember for sure if it was C# code I
saw it in. Apparently not.

But Delphi DOES support this kind of construct. Very convenient.

....Arjan...
 
Reply With Quote
 
Peter Morris
Guest
Posts: n/a
 
      8th May 2009
PRISM does it in .NET

http://prismwiki.codegear.com/en/Implements_(keyword)

I expect it does the manual code automatically.




--
Pete
====
http://mrpmorris.blogspot.com
http://www.AlterEgos.com - Who do you want to be?
 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      11th May 2009
Personally I am at aloss as to why this would ever be useful.

Where do I start with the example. It makes no sense for Outer to even
implement the interface in any case if it is not implementing the methods.
It clearly is not a IMyInterface.

If it is an aggregated object then why Aggregate at type level?

From what you have described you seem to be creating some wierd and
wonderful Composite pattern one that until I understand differently Im glad
they left out of C#.

Yes all these techy things are a bit of fun but does it actually reflect
something you are creating for the real world.


"Arjan de Haan" <(E-Mail Removed)_this_too.nl> wrote in
message news:(E-Mail Removed)...
> Hi.
>
> I'm at a loss here. I seem to remember that it is possible to allow a
> nested class to implement an interface that is declared on the outer
> class, like this:
>
> public class Outer : IMyInterfave
> {
> private readonly Inner _innerImpl = new Inner();
>
> private class Inner : IMyInterface
> {
> public int FirstMethod()
> {
> ...
> }
>
> public void SecondMethod()
> {
> ...
> }
> }
>
> public void OtherMethod()
> {
> ...
> }
>
> //
> // The next 2 methods I DONT want
> //
> public int FirstMethod()
> {
> return _innerImpl.FirstMethod();
> }
>
> public void SecondMethod()
> {
> return _innerImpl.SecondMethod();
> }
>
> }
>
> I know I can get the behavior I want if I implement FirstMethod() and
> SecondMethod() in Outer and call the same methods on the instance of the
> inner class (see above).
> But, would this be possible with help from the compiler? In other words,
> would it be possible to tell (using Attribites?) to tell the compiler that
> the interface IMyInterface for class Outer is implemented using Inner? If
> so, that would mean the methods for the interface as declared in Outer
> would not be necessary. Perhaps by introducing a property of the Interface
> and adding a Attribute to it, like so:
>
> public class Outer : IMyInterface
> {
> private readonly Inner _innerImpl = new Inner();
>
> private class Inner : IMyInterface
> {
> ...
> ...
> }
>
> public void OtherMethod()
> {
> ...
> }
>
> [Implements(IMyInterface)]
> public Inner InnerClass { get { return _innerImpl; } }
> }
>
> Now I don't how to write the boring IMyInterface methods and delegation to
> the inner class.
>
> Is this possible using C#?
>
> Tx.
> ...Arjan...



 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      11th May 2009
Personally I am at aloss as to why this would ever be useful.

Where do I start with the example. It makes no sense for Outer to even
implement the interface in any case if it is not implementing the methods.
It clearly is not a IMyInterface.

If it is an aggregated object then why Aggregate at type level?

From what you have described you seem to be creating some wierd and
wonderful Composite pattern one that until I understand differently Im glad
they left out of C#.

Yes all these techy things are a bit of fun but does it actually reflect
something you are creating for the real world.


"Arjan de Haan" <(E-Mail Removed)_this_too.nl> wrote in
message news:(E-Mail Removed)...
> Hi.
>
> I'm at a loss here. I seem to remember that it is possible to allow a
> nested class to implement an interface that is declared on the outer
> class, like this:
>
> public class Outer : IMyInterfave
> {
> private readonly Inner _innerImpl = new Inner();
>
> private class Inner : IMyInterface
> {
> public int FirstMethod()
> {
> ...
> }
>
> public void SecondMethod()
> {
> ...
> }
> }
>
> public void OtherMethod()
> {
> ...
> }
>
> //
> // The next 2 methods I DONT want
> //
> public int FirstMethod()
> {
> return _innerImpl.FirstMethod();
> }
>
> public void SecondMethod()
> {
> return _innerImpl.SecondMethod();
> }
>
> }
>
> I know I can get the behavior I want if I implement FirstMethod() and
> SecondMethod() in Outer and call the same methods on the instance of the
> inner class (see above).
> But, would this be possible with help from the compiler? In other words,
> would it be possible to tell (using Attribites?) to tell the compiler that
> the interface IMyInterface for class Outer is implemented using Inner? If
> so, that would mean the methods for the interface as declared in Outer
> would not be necessary. Perhaps by introducing a property of the Interface
> and adding a Attribute to it, like so:
>
> public class Outer : IMyInterface
> {
> private readonly Inner _innerImpl = new Inner();
>
> private class Inner : IMyInterface
> {
> ...
> ...
> }
>
> public void OtherMethod()
> {
> ...
> }
>
> [Implements(IMyInterface)]
> public Inner InnerClass { get { return _innerImpl; } }
> }
>
> Now I don't how to write the boring IMyInterface methods and delegation to
> the inner class.
>
> Is this possible using C#?
>
> Tx.
> ...Arjan...



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Explicit interface implementation from abstract class KH Microsoft C# .NET 2 6th May 2009 09:25 PM
inherited class instead of expected class in interface implementation parez Microsoft C# .NET 2 9th May 2008 04:26 PM
Trouble with C++ class Inheriting from a C# parameterized class with explicit interface implementation Howard Swope Microsoft VC .NET 5 7th Feb 2008 06:14 PM
Re: explicit implementation of interface in a derived class 100 Microsoft C# .NET 2 13th Aug 2003 09:36 PM
Re: explicit implementation of interface in a derived class Ashwin Kambli Microsoft C# .NET 3 13th Aug 2003 09:32 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:56 PM.