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 7946689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:13:21+00:00 2026-06-04T01:13:21+00:00

I have two classes that are derived from an abstract generic class, which is

  • 0

I have two classes that are derived from an abstract generic class, which is derived from System.Web.UI.Page. I have a System.Web.UI.MasterPage that may or may not be the master for either of my two derived page classes, or System.Web.UI.Page.

The problem is that the generic class has a property that I need to access in my MasterPage, but I don’t know any elegant way to get it.

Here is an example…

Content types:

public abstract class Fruit
{
    public int ID { get; set; } //Just an identifier
}

public class Apple : Fruit {  }

public class Banana : Fruit {  }

Pages:

public abstract class FruitPage<T> : System.Web.UI.Page where T : Fruit
{
    public T MyFruit { get; set; } 
}

public class ApplePage : FruitPage<Apple> {  }

public class BananaPage : FruitPage<Banana> {  }

Master:

public partial class FoodMaster : System.Web.UI.MasterPage
{
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        if (this.Page is FruitPage<Fruit>) //I know this is wrong
        {
            if ((this.Page as FruitPage<Fruit>).MyFruit.ID <= 0) //This too
            { 
                /*
                    I want to get here (this.Page being an ApplePage or BananaPage).  

                    Basically...  if ID<=0 then it is a new (unsaved) "fruit", 
                        and I need to change some MasterPage GUI accordingly. 
                */
            }
        }
    }
}

Thank you!

  • 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-06-04T01:13:23+00:00Added an answer on June 4, 2026 at 1:13 am

    “Any problem in computer science can be solved with another level of indirection.”

    public class FruitPage : System.Web.UI.Page {
       public Fruit MyInnerFruit { get; protected set; }
    }
    
    public abstract class FruitPage<T> : FruitPage where T : Fruit
    {
       public T MyFruit { 
         get { return (T)MyInnerFruit; } 
         set { MyInnerFruit = value; } 
       }
    }
    
    public partial class FoodMaster : System.Web.UI.MasterPage {
        protected override void OnLoad(EventArgs e) {
           base.OnLoad(e);
    
           var fruitPage = this.Page as FruitPage;
           if (fruitPage != null && fruitPage.MyInnerFruit.ID <= 0) {
              ...
           }
        }
    }
    

    You could also shadow MyInnerFruit/MyFruit – but it’s easier to understand without.

    If you’re using .NET 4, you can also use a covariant interface to basically allow casts from FruitPage<Apple> to FruitPage<Fruit>:

    public interface IFruitPage<out T> where T : Fruit {
        public T MyFruit { get; }
    }
    
    public class FruitPage<T> : Page, IFruitPage<T> where T : Fruit {
        public T MyFruit { get; set; }
    }
    
    public class FoodMaster : MasterPage {
        protected override void OnLoad(EventArgs e) {
           base.OnLoad(e);
    
           var fruitPage = this.Page as IFruitPage<Fruit>;
           if (fruitPage != null && fruitPage.MyFruit.ID <= 0) {
              ...
           }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes, Person and Company, derived from another class Contact. They are
I have an abstract class MainClass which composes Animal class. I derive two classes
I have two Parser classes that inherit from a base class, BaseParser. I want
I'm having trouble grasping generic methods. I have two classes that are generated (they
Say we have a class inheriting from two base classes (multiple inheritance). Base class
I have two classes, Foo<T> and Bar<T> , derived from Base . Each overrides
I have two classes, a base class and a derived class. My base class
I have two classes: a base class, Foo::Base and a derived class, Foo::Base::Sub .
I have a number of classes that derive from a pure virtal base: class
I have an abstract class called DatabaseRow that, after being derived and constructed, is

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.