4

I'm trying to understand why the following will not compile. The compiler complains on the line where CreatePresenter tries to set the View property:

Cannot implicitly convert type 'Sandbox.Program.MyView' to 'TView'.

I know the context of the assignment does not make sense, it's more for illustration. Any help would be great!

    interface IView {
    }

    class Presenter<T> where T : IView {
        public T View { get; set; }
    }

    class MyView : IView {
    }

    class MyPresenter : Presenter<MyView> {
        public MyPresenter() { }
    }

    class ViewBase<TPresenter, TView>  
        where TPresenter : Presenter<TView>, new()
        where TView : IView {

        public TPresenter Presenter { get; private set; }

        void CreatePresenter() {
            this.Presenter = new TPresenter();
            this.Presenter.View = new MyView();
        }
    }
4
  • What version of .NET are you using?
    – Oded
    Commented Mar 1, 2011 at 20:34
  • Must the presenter's view always be an instance of the concrete MyView?
    – BoltClock
    Commented Mar 1, 2011 at 20:35
  • @Michael - he posted it. Cannot convert type.
    – Oded
    Commented Mar 1, 2011 at 20:35
  • @BoltClock Like I said, this is for illustration purposes only. The assignment would really be to 'this'
    – Marco
    Commented Mar 1, 2011 at 20:37

3 Answers 3

7

On the line:

this.Presenter.View = new MyView();

you're trying to set a MyView object to a property of generic type TView (implementing IView).

This is wrong because MyView is different from TView so it can't be assigned.

EDIT:

To expand a bit...

You can assign a type to its interface e.g. :

IView v = new MyView(); // OK !

but you can't assign an interface to one of its implementers e.g.:

IView v = ...;
MyView myView = v; // WRONG !

Here you're doing something more convoluted, something like this:

        IView
   _______|_______
  |               |
  |               |
TView    <--   MyView

That is clearly wrong.
In fact, even if TView and MyView implement the same interface, to allow this TView should inherit from MyView but that's not specified among the generic constraints, and so the compiler can't do any assumption.

Have a look also to davy8's answer for a clear example :)

5
  • I was about to submit my answer when this appeared with a V similar description ;) +1.
    – OJ.
    Commented Mar 1, 2011 at 20:38
  • even though MyView implements IView ?
    – Marco
    Commented Mar 1, 2011 at 20:38
  • 1
    @Marco that's irrelevant because for example both int and BigInt implement IComparable, you can't set the BigInt to int. Commented Mar 1, 2011 at 20:40
  • @Yuriy ok, that makes sense. i am just learning about co/contra variance and i thought this would allow this type of code.
    – Marco
    Commented Mar 1, 2011 at 20:43
  • @Marco see my answer for a concrete example of why it can't work.
    – Davy8
    Commented Mar 1, 2011 at 20:49
1

Imagine you have 2 more classes:

class MyView2 : IView
{
}

class MyPresenter2 : Presenter<MyView2>
{
    public MyPresenter2() { }
}

Then you try to construct a new ViewBase with those:

var viewBase = new ViewBase<MyPresenter2, MyView2>();

In that case, in your method:

void CreatePresenter()
{
    this.Presenter = new TPresenter();
    this.Presenter.View = new MyView();
}

this.Presenter.View is MyView2 which doesn't inherit from MyView, even though both of them implement IView

0
0

MyView is not guaranteed to be of the same type TView. You can have several implementations of IView that aren't MyViews using this generic class.

If you change ViewBase to the code below, it will at least compile. I'm not sure how it will affect the behavior of your application.

class ViewBase<TPresenter, TView>
    where TPresenter : Presenter<TView>, new()
    where TView : IView, new()
{

    public TPresenter Presenter { get; private set; }

    void CreatePresenter()
    {
        this.Presenter = new TPresenter();
        this.Presenter.View = new TView();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.