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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:38:49+00:00 2026-06-01T12:38:49+00:00

I’m trying to get my head around a polymorphism/inheritance situation in C#. What I

  • 0

I’m trying to get my head around a polymorphism/inheritance situation in C#.

What I have right now is these classes:

Lease (the base class containing the general data)
PrivateLease (inheriting from the Lease class)
BusinessLease (inheriting from the Lease class)

What I want to achieve is this:

Lease lease = new PrivateLease();

This works at the moment, but I am not able to access the properties on the PrivateLease object when doing this. At least not without casting the Lease object to a PrivateLease object first.

I’d like the Lease object to be the general object of either a PrivateLease or BusinessLease object which holds all the data for one of the objects. Then when inserting/updating/deleting to the database I’m going to ask which type it is first to dertermine which tables to insert the data into.

I’ve got a strange feeling that the above is not the right approach to solve this problem. Does anyone have any hints on this? 🙂 I’ve searched on google and read in my programming books and everyone suggests this approach of having a base class and then inherit from it to the other classes.

Any help/hint is greatly appreciated!

Thanks in advance.

EDIT

Should’ve elaborated a bit on this from the beginning, I’m sorry for that!

The above mentioned classes are merely just holding data from the UI of my ASP.NET solution to perform CRUD operations against the database via a Data Access Layer. So bascially these classes only contains a bunch of properties to hold data. I.e:

public class Lease
{
    public int Id { get; set; }
    public bool IsActive { get; set; }
    public string TypeOfRental { get; set; }
    public string RentalPeriod { get; set; }
    public DateTime TakeoverDate { get; set; }        
}

public class PrivateLease : Lease
{
    public string Floor { get; set; }
    public string Side { get; set; }
    public int FloorSize { get; set; }
    public int NumberOfRooms { get; set; }
}

etc..

The PrivateLease and BusinessLease classes are different because of the different leaseing-variables that exists in the real world 🙂

Basically I could just go with the two separate PrivateLease and BusinessLease objects, but since the model dictates that an Address object can hold one or more Leases, this is not an option.

To me it seems like I’m going to go through a major casting hell both on the ASP.NET frontend and on the DAL? :-/

  • 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-01T12:38:50+00:00Added an answer on June 1, 2026 at 12:38 pm

    Don’t decide (choose a logic) on the layer of consumer, but let to decide by the classes themselves:

    // or you ILease interface if a parent class will not contain any shared logic
    abstract class Lease
    {
        public abstract void Do();
    
        // example of shared logic
        protected void Save(Lease l) { }
    }
    
    class PrivateLease : Lease
    {
        public override void Do() { // private logic here }
    }
    
    class BusinessLease : Lease
    {
        public override void Do() { // business logic here }
    }
    

    Usage:

    Lease l = ...
    l.Do(); // execute the logic
    

    You may want to create a factory for objects creation:

    static class LeaseFactory<T> where T : Lease, new() // constraint to require default constructor existence
    {
        public static Leas Create()
        {
            return new T();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is what i have right now Drawing an RSS feed into the php,
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to loop through a bunch of documents I have to put
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.