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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:00:55+00:00 2026-05-31T09:00:55+00:00

I have an OO problem here. I have two sessions which has common properties

  • 0

I have an OO problem here. I have two sessions which has common properties and specific properties. I created a base class and encapsulated all the common properties/methods.
Two session has a common type called Ranges, which again has common property and specific property for the sessions. Hence I thought I could program to supertype in this case and build the instance at runtime.

public class Level
{
    private readonly Ranges _range;

    public Level(Ranges range)
    {
        _range = range;
    }

    public Ranges Range
    {
        get { return _range; }
    }

    public void CommonMethod()
    {
        throw new NotImplementedException();
    }

    public int CommonProperty;
}

public class ALevel : Level
{
    public ALevel()
        : base(new ARange())
    {

    }

    public int ALevelProperty;
}

public class BLevel : Level
{
    public BLevel()
        : base(new BRange())
    {

    }

    public int BLevelProperty;
}

public class Ranges
{
    public int CommonRangeProperty;
}

public class ARange : Ranges
{
    public int ARangeProperty;

    public ARange() 
    {

    }
}

public class BRange : Ranges
{
    public int BRangeProperty;
}

public class ASession
{
    public ASession()
    {
        Level = new ALevel();
    }

    public ALevel Level { get; set; }
}

public class BSession
{
    public BSession()
    {
        Level = new BLevel();
    }

    public BLevel Level { get; set; }
}

When I create a session object, it doesn’t contain specific Ranges property of ASession.
I can access only the base class’s property
aSession.Level.Range.CommonRangeProperty = 1;
but am not able to access the aSession’s specific property
aSession.Level.Range.ARangeProperty.

Am I doing something wrong in here?

public class Test
{
    public static void Main(string[] args)
    {
        ASession aSession = new ASession();

        aSession.Level.Range.CommonRangeProperty = 1;
        //Not able to see ARangeProperty

    }
}
  • 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-31T09:00:56+00:00Added an answer on May 31, 2026 at 9:00 am

    it’s pretty simple:

    your class Level sets the type of Range to Ranges (not the specific ARange nor BRange).
    you should work with generics, eg:

    public abstract class Level<TRange>
       where TRange : Ranges
    {
        private readonly TRange _range;
        protected Level(TRange range)
        {
            this._range = range;
        }
        public TRange Range
        {
            get
            {
                return this._range;
            }
        }
    }
    
    public class ALevel : Level<ARange>
    {
        public ALevel()
            : base (new ARange())
        {
        }
    }
    

    you can take this example even further:

    public abstract class Level<TRange>
       where TRange : Ranges/*, new()*/ // i'm not sure about the new() ... have no compiler access right now to check!
    {
        private readonly TRange _range = new TRange();
        public TRange Range
        {
            get
            {
                return this._range;
            }
        }
    }
    
    public class ALevel : Level<ARange>
    {
    }
    

    our you could introduce another member in ALevel, like so:

    public class ALevel : Level
    {
       public ARange ARange;
    }
    

    you could enhance this example by making Range in Level virtual and overriding Range (with a redirect to the concrete ARange or BRange) in the concrete Levels.

    it heavily depends on the usage afterwards…
    if you need generic access to the proprety Range as Ranges you should introduce another base class (without generic constraint) to introduce a base member, which you can override in your generic base class. so you can cast an instance of ALevel to Level (w/o the base class you would have to cast to Level<TRange> which the knowledge of the concrete TRange) …

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

Sidebar

Related Questions

I have a bit of an architecture problem here. Say I have two tables,
Here's my problem at a high level: We have two business applications. App1 inputs
I have run into a bit of a problem here: I had a problem-specific
I'm having a strange problem here... I have an ASP.NET 3.5 application that has
I'm currently facing a strange problem in PHP. I have two scripts, index.php which
Greetings, Here's the problem I'm having. I have a page which redirects directly to
I have a problem here. My Zend_Forms do not render in view script. Via
Using Rational ClearCase v. 7.0.1.1 with UCM, I have a problem here when using
I have an interesting problem here I've been trying to solve for the last
I'm having a bit of a problem here. We have 2 urls let me

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.