Extending Return Types ?? 
Author Message
 Extending Return Types ??

I've finally finished my exams and now have a bit of time to play about with
C#, however I've hit a snag.

Please have a look at the code below.
public interface ITreeBase{

    ITreeBase Parent{get;}

Quote:
}

public class BaseTreeControl : Control, ITreeBase{

    public BaseTreeControl Parent{

        get{

            return (BaseTreeControl)base.Parent; // base.Parent type =
Control

        }

    }

Quote:
}

When I try to compile I am told that I haven't implemented the ITreeBase
interface properly. My thinking is that BaseTreeControl derives from Control
and implements ITreeBase so should be acceptable for a return type. I've
tried using new and override but these don't work either.

Is what I'm trying to do possible using some other method I'm unaware?

Thanks for reading.

    Damien



Sat, 20 Nov 2004 01:15:15 GMT  
 Extending Return Types ??
Damien,

    Your implementation of Parent is defined to return an implementation of
a class, not an interface instance, that is why you are getting that error.
You want your declaration to be this:

public ITreeBase Parent

    This way you match the property parameter list and return type
correctly, fufilling the requirements to implement the interface.

    Hope this helps.

--
               - Nicholas Paldino [.NET MVP]


Quote:
> I've finally finished my exams and now have a bit of time to play about
with
> C#, however I've hit a snag.

> Please have a look at the code below.
> public interface ITreeBase{

>     ITreeBase Parent{get;}

> }

> public class BaseTreeControl : Control, ITreeBase{

>     public BaseTreeControl Parent{

>         get{

>             return (BaseTreeControl)base.Parent; // base.Parent type =
> Control

>         }

>     }

> }

> When I try to compile I am told that I haven't implemented the ITreeBase
> interface properly. My thinking is that BaseTreeControl derives from
Control
> and implements ITreeBase so should be acceptable for a return type. I've
> tried using new and override but these don't work either.

> Is what I'm trying to do possible using some other method I'm unaware?

> Thanks for reading.

>     Damien



Sat, 20 Nov 2004 02:03:30 GMT  
 Extending Return Types ??


Quote:
> What you are trying to do is called a "covariant return". It is not
> supported in C#. Try [1] for a selection of the discussions on this.

> --
> Arild Fines

[1]http://groups.google.com/groups?hl=en&lr=&q=covariant&btnG=Google+Sea...

Quote:
> meta=group%3Dmicrosoft.public.dotnet.languages.csharp

That's a pity, I'm no OOP guru but it seems to me it should be. If you can
pass a method a derived type of the argument(s) then why shouldn't you be
able to have covariant returns, after all it would be type safe? If anyone
knows why it's been omitted from C# could you give an explanation please.

Cheers Damien



Sat, 20 Nov 2004 04:32:39 GMT  
 Extending Return Types ??
McGiv,
"Passing a method a derived type of the argument" is different than
"Defining an overloaded method with a derived type of the argument". The
later is also not supported!

// Sample of covariant parameters, which are NOT supported in C#

public class A { }
public class B : A { }

class Base
{
    public virtual void MyMethod(A a) { }

Quote:
}

class Derived : Base
{
    // this method would override Base.MyMethod(A)
    public override void MyMethod(B a) { }

Quote:
}

NOTE: Eiffel supports both 'covariant return' and 'covariant parameters'.
See http://www.eiffel.com There is a version of Eiffel for .NET complete
with covariants.

Unmanaged C++ supports covariant returns...

Hope this helps
Jay


Quote:


message

> > What you are trying to do is called a "covariant return". It is not
> > supported in C#. Try [1] for a selection of the discussions on this.

> > --
> > Arild Fines

[1]http://groups.google.com/groups?hl=en&lr=&q=covariant&btnG=Google+Sea...
Quote:
> > meta=group%3Dmicrosoft.public.dotnet.languages.csharp

> That's a pity, I'm no OOP guru but it seems to me it should be. If you can
> pass a method a derived type of the argument(s) then why shouldn't you be
> able to have covariant returns, after all it would be type safe? If anyone
> knows why it's been omitted from C# could you give an explanation please.

> Cheers Damien



Sat, 20 Nov 2004 07:35:39 GMT  
 Extending Return Types ??

Quote:
> "Passing a method a derived type of the argument" is different than
> "Defining an overloaded method with a derived type of the argument". The
> later is also not supported!

and neither are contravariant parameter types, which would be useful too.
further, contravariant parameters are typesafe while covariant parameters
are not.

Quote:
> // Sample of covariant parameters, which are NOT supported in C#

interestingly, introducing covariant and/or contravariant parameters to C#
(eg. in version 2) would potentially break existing C# code. introducing
covariant returns would not, however, so there's slightly more hope that
it'll be included in a revision to the language.

cheers,

dave



Quote:
> McGiv,
> "Passing a method a derived type of the argument" is different than
> "Defining an overloaded method with a derived type of the argument". The
> later is also not supported!

> // Sample of covariant parameters, which are NOT supported in C#

> public class A { }
> public class B : A { }

> class Base
> {
>     public virtual void MyMethod(A a) { }
> }

> class Derived : Base
> {
>     // this method would override Base.MyMethod(A)
>     public override void MyMethod(B a) { }
> }

> NOTE: Eiffel supports both 'covariant return' and 'covariant parameters'.
> See http://www.eiffel.com There is a version of Eiffel for .NET complete
> with covariants.

> Unmanaged C++ supports covariant returns...

> Hope this helps
> Jay




> message

> > > What you are trying to do is called a "covariant return". It is not
> > > supported in C#. Try [1] for a selection of the discussions on this.

> > > --
> > > Arild Fines

[1]http://groups.google.com/groups?hl=en&lr=&q=covariant&btnG=Google+Sea...

- Show quoted text -

Quote:
> > > meta=group%3Dmicrosoft.public.dotnet.languages.csharp

> > That's a pity, I'm no OOP guru but it seems to me it should be. If you
can
> > pass a method a derived type of the argument(s) then why shouldn't you
be
> > able to have covariant returns, after all it would be type safe? If
anyone
> > knows why it's been omitted from C# could you give an explanation
please.

> > Cheers Damien



Sat, 20 Nov 2004 19:46:52 GMT  
 Extending Return Types ??

Quote:
>If anyone knows why it's been omitted from C# could you give an explanation

please.

It is one of the questions scheduled for "Ask a language designer"[1]

--
Arild Fines

[1]http://www.gotdotnet.com/team/csharp/Information/ask_a_language_designer.
aspx



Mon, 31 Jan 2005 05:15:45 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Help with: C2392 - covariant returns types are not supported in managed types

2. undefined type in arg list vs. return type

3. HRESULT return type turns into void when adding class from type library

4. Setting a numeric return value in an extended stored procedure

5. Extending ATL Server Webservices Data Type support

6. Extended integer types

7. big int and extended data types

8. Extending CxxxView by extending CxxxCtrl

9. Extending CxxxView by extending CxxxCtrl

10. C++ .NET 2003 conformance - covariant return types

11. Explicit interface members with different return types in MC++

12. Covariant return types for Properties

 

 
Powered by phpBB® Forum Software