Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6199719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:14:19+00:00 2026-05-24T04:14:19+00:00

Background: I have three Silverlight Pages, that implement my interface: interface IPageWithData<in T> where

  • 0

Background:

I have three Silverlight Pages, that implement my interface:

interface IPageWithData<in T> where T : Entity
{
    void SetData(T obj);
}

public class APage : Page, IPageWithData<AItem> { /* Implementation */ }
public class BPage : Page, IPageWithData<BItem> { /* Implementation */ }
public class CPage : Page, IPageWithData<CItem> { /* Implementation */ }

// AItem, BItem and CItem are (EF) Entities (they are derived from Entity class).

Then I use a custom manager that returns a page that relates to the treeview node given as a parameter:

private static class PageContainerHelper
{
    public static IPageWithData<Entity> SetPage(RadTreeViewItem selectedNode)
    {
        APageRTVI aNode = selectedNode as APageRTVI;
        BPageRTVI bNode = selectedNode as BPageRTVI;
        CPageRTVI cNode = selectedNode as CPageRTVI;

        if (aNode != null)
        {
            if (APage == null)
                APage = new APage();

            APage.SetData(aNode.aItem);
            return APage; // ERROR HERE
        }

        /*
            ... the same for BPageRTVI and CPageRTVI
        */
    }

    public static APage APage { get; set; }
    public static BPage BPage { get; set; }
    public static CPage CPage { get; set; }
}

Problem:

I seem to be understanding co/contravariance the wrong way.
The SetPage() method doesn’t allow me to return APage:

Cannot implicitly convert type ‘MyProject.Views.Pages.APage’ to ‘MyProject.Views.Pages.IPageWithData’. An explicit conversion exists (are you missing a cast?)

APage implements IPageWithData<T> (albeit T is of a more derived type). Why does this require a explicit conversion? And would an explicit conversion even work here?

So basically, there are two things I need. One is that when SetPage is returning a page, it can use a page that implements IPageWithData of a type that is more derived than Entity. And the other thing I need is for the IPageWithData<T>.SetData method to be able to receive arguments that are of a type more derived than a T, that is Entity.

Is this possible?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T04:14:20+00:00Added an answer on May 24, 2026 at 4:14 am

    Consider what you can do with IPageWithData<Entity>. You can do:

    IPageWithData<Entity> pageWithData = PageContainerHelper.SetPage(...);
    BItem item = new BItem();
    pageWithData.SetData(item);
    

    Now the implementation is an APage – what would you want it to be able to do with a BItem? I suspect you don’t want that.

    In other words, the compiler is doing exactly what it should – it’s protecting you from sending the wrong kind of data to a page which can’t handle it. Quite how you want to fix this, I don’t really know – I suspect you want SetPage to be generic too:

    public static IPageWithData<T> SetPage<T>(RadTreeViewItem selectedNode)
        where T : Entity
    

    but it’s not clear to me what that does to your implementation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background: I have a kubuntu laptop right now that I can't use wirelessly, i.e.
Background: we have an application that generates reports from HTML (that may or may
Background: We have an offshore group working up a Silverlight 2 prototype for us.
Background: I work in a suite of ASP.NET applications that have several different modules.
I have a Silverlight app with two XAML Pages - MainPage and OtherPage How
I have a Silverlight app that takes a few seconds to display on a
I have a toggle button with a png that has a transparent background and
I have a Silverlight application that needs Drag-And-Drop functionality because I allow the user
I have a background that I need fit in all screen sizes. I have
I have three images (320x480) that I'm trying to scroll vertically in my Cocos2D

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.