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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:34:58+00:00 2026-05-23T10:34:58+00:00

I have base class and two inherited classes, these two inherited classes also share

  • 0

I have base class and two inherited classes, these two inherited classes also share the two properties. I would like to access these properties from base class. How to do that? Example below.

public abstract class ClientEngine
{
        public void SaveFile(string fileName);
        {
            //Not implemented, I get error here because I don't know how to access Inherited class properties
            if (FileStorage != null))
                File.WriteAllBytes(fileName, FileStorage);
        }
}

    public interface IEngineEntity
    {
        byte[] FileStorage { get; set; }
    }

public class MyEntity1 : ClientEngine, IEngineEntity
{

public string MyProperty1 {get; set;}
public string MyProperty2 {get; set;}

public byte[] FileStorage { get; set; }
}

public class MyEntity2 : ClientEngine, IEngineEntity
{

public string MyProperty3 {get; set;}
public string MyProperty4 {get; set;}

public byte[] FileStorage { get; set; }
}
  • 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-23T10:34:59+00:00Added an answer on May 23, 2026 at 10:34 am

    If you need to access a property from the base class, you are doing something wrong. Keep that in mind for your future designs.

    You should declare FileStorage in the base class, since both inherited classes have it.

    If you do not want to define FileStorage‘s specific type (say, byte[]), you might want to define it as a more generic type, such as IEnumerable<byte>. Alternatively, you can declare FileStorage as an interface who provides whatever behavior you need to access from base class.

    Update

    A nice observation by @ghimireniraj: you should also try to derive ClientEngine from IEngineEntity. One of the bottom lines for OO design is that if you replicate behavior in all inherited classes, it should be in the base class.

    But, do you really want to access some code from base class? You shouldn’t. But if you really need, you can always cast:

    public abstract class ClientEngine
    {
        public void SaveFile(string fileName);
        {
            byte[] fileStorage = null;
            if (this is MyEntity1)
            {
                MyEntity1 me1 = (MyEntity1)this;
                fileStorage = me1.FileStorage;
            }
            else if (this is MyEntity2)
            {
                MyEntity2 me2 = (MyEntity2)this;
                fileStorage = me2.FileStorage;
            }
            if (fileStorage != null))
                File.WriteAllBytes(fileName, fileStorage);
        }
    }
    

    Nasty right? But it should look dirty, because we are trying to break OO design.

    Update

    Another way is going by Marc Gravell’s suggestion. Instead of casting to the types, cast to the interface. But all of this still smells bad to me: I firmly believe you can improve your design.

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

Sidebar

Related Questions

Let's say we have these two classes: public class Base { public static int
I have two classes that my pages and controls inherit from. These two classes
I have a class base on my map, it inherited two new classes <?xml
Suppose I have two classes a base class and an inherited class as follows:
I have two classes. The base class is A. The inherited class is B.
I have a question, here are two classes below: class Base{ public: virtual void
I have a code base, in which for Matrix class, these two definitions are
I have a two models set up like this: class User < ActiveRecord::Base #
I'm designing/building a system of classes that all derive from a single base class.
VB.NET 2008 .NET 3.5 I have two base classes that are MustInherit (partial). Let's

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.