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

  • SEARCH
  • Home
  • 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 523173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:25:49+00:00 2026-05-13T08:25:49+00:00

I wrote ASP.NET pages which will manage forms. They’re based on the following base

  • 0

I wrote ASP.NET pages which will manage forms. They’re based on the following base class.

public abstract class FormPageBase<TInterface, TModel> : Page, IKeywordProvider 
        where TModel:ActiveRecordBase<MasterForm>, TInterface, new()
        where TInterface:IMasterForm
    {
        public TInterface FormData { get; set; }                   
     }

And a sample SubClass is here:

public partial class PersonalDataFormPage : FormPageBase<IPersonalDataForm, PersonalDataForm>, IHasFormData<IPersonalDataForm>, IHasContact
    {
    }

Below I have a usercontrol on the page which I want to “consume” the “FormData” from the page so that it can read/write to it.

I then, have a more “common” user control that I want to have operate on the base Interface of all my form subclasses… IMasterForm

But when the usercontrol tries casting Page.FormData (having tried to cast page to IHasFormData<IMasterForm> it tells me that the page is IHasFormData<IFormSubclass> even though I have a constraint on the IFormSubclass that says it is also IMasterForm

Is there anyway that i can cast from the generic subclass to the generic superclass or is this “covariance” and a C# 4.0 thing?

public abstract class FormControlBase<T> : UserControl, IKeywordProvider
    where T:IMasterForm 
{

    protected T FormData { get; set; }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

//This cast is failing when my common control's T does not exactly match
// the T of the Page.. even though the common controls TInterface is a base interface to the
//pages TInterface

        FormData = ((IHasFormData<T>) Page).FormData;

        if (!IsPostBack)
        {
            PopulateBaseListData();
            BindDataToControls();
        }
    }

    protected abstract void PopulateBaseListData();
    protected abstract void BindDataToControls();


    public abstract void SaveControlsToData();


    #region IKeywordProvider
    public List<IKeyword> GetKeywords(string categoryName)
    {
        if(!(Page is IKeywordProvider ))
            throw new InvalidOperationException("Page is not  IKeywordProvider");

        return ((IKeywordProvider) Page).GetKeywords(categoryName);
    }

    #endregion

}
  • 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-13T08:25:49+00:00Added an answer on May 13, 2026 at 8:25 am

    Let me first see if I can restate this complicated problem more succinctly. You have a generic interface IHasFormData<T>. You have an object which is known to implement IHasFormData<IFormSubclass>. You wish to convert it to IHasFormData<IMasterForm>. You know that there is a reference conversion from IFormSubclass to IMasterForm. This fails.

    Yes?

    If that is a correct statement of the problem, then yes, this is a question of interface covariance. C# 3 does not support interface covariance. C# 4 will, if you can prove to the compiler that covariance is safe.

    Let me describe for you briefly why this might not be safe. Suppose you have classes Apple, Orange and Fruit with the obvious subclassing relationships. You have an IList<Apple> which you would like to cast to IList<Fruit>. That covariant conversion is not legal in C# 4 and cannot be legal because it is not safe. Suppose we allowed it. You could then do this:

    IList<Apple> apples = new List<Apple>();
    IList<Fruit> fruits = apples;
    fruits.Add(new Orange()); 
    // We just put an orange into a list of apples!  
    // And now the runtime crashes.
    

    Notice that the problem is that List<T> exposes a method that takes a T as an argument. In order for the compiler to allow covariant conversions on your interface IHasFormData<T>, you must prove to the compiler that IHasFormData<T> exposes nothing that takes a T as an argument. You’ll do that by declaring the interface IHasFormData<out T>, a mnemonic meaning “T only appears in output positions”. The compiler will then verify that your claim is correct, and start allowing the covariant conversions.

    For more information on this feature in C# 4, see my archive of notes on the design of the feature:

    http://blogs.msdn.com/ericlippert/archive/tags/Covariance+and+Contravariance/default.aspx

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

Sidebar

Ask A Question

Stats

  • Questions 271k
  • Answers 271k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I wouldn't. I'd rely on the user's ID provider to… May 13, 2026 at 1:39 pm
  • Editorial Team
    Editorial Team added an answer The easiest (but probably not the fastest) way is to… May 13, 2026 at 1:39 pm
  • Editorial Team
    Editorial Team added an answer I'm not really sure Tim addressed your question so I'll… May 13, 2026 at 1:39 pm

Related Questions

I'm using jQuery UI's draggable and droppable libraries in a simple ASP.NET proof of
How do I get the collection of all Session objects in ASP.NET? (Note that
I've been trying for a while now to write a unit test for a
I am trying to figure out how to notify the user which field failed
IIS enables us to also configure Asp.Net file mappings. Thus besides aspx, IIS also

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.