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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:43:33+00:00 2026-06-10T04:43:33+00:00

I’ve been working on building several classes which inherit from one base class but

  • 0

I’ve been working on building several classes which inherit from one base class but I’m not entirely confident on how inheritance and polymorphism work in C# at this stage.

My base class looks like this:

abstract class Structure
    {
        public int currentCost = 0;
        public int currentArea = 0;
        public int currentPopulation = 0;
        public int currentConstruction = 0;
        public int currentEnergy = 0;
        public int currentEconomy = 0;

        public abstract int baseCost { get; }
        public abstract int baseEnergy { get; }
        public abstract int baseEconomy { get; }
        public abstract int baseConstruction { get; }
        public int baseArea = -1;
        public int basePopulation = -1;

        public int level = 0;
        public abstract string structureName { get; }
}

Now, classes that inherit from the Structure class will be made to provide their own assignments for the abstract variables which is fine as most of the classes vary wildly in the figures they assign.
The abstract variables are used in the derived classes in the following (incomplete) manner:

class BiosphereModification : Structure
    {
        const int baseEconomyBiosphereModification = 0;
        const int baseConstructionBiosphereModification = 0;
        const int baseCostBiosphereModification = 2000;
        const int baseEnergyBiosphereModification = 0;
        const int baseFertilityBiosphereModification = 1;
        const string structureNameBiosphereModification = "BiosphereModification";

        public override int baseCost { get { return baseCostBiosphereModification; } }
        public override int baseEconomy { get { return baseEconomyBiosphereModification; } }
        public override int baseEnergy { get { return baseEnergyBiosphereModification; } }
        public override int baseConstruction { get { return baseConstructionBiosphereModification; } }
}

However, the non-abstract variables will be the same across the majority of derived classes, but not all of them.

I could make them all abstract and force each class to provide it’s own value, but this seems counter-intuitive. What I would prefer is a way to provide a value in the base class and provide an override in a derived class if needed.

Is there a way to do this? I know that this can be done with methods declared virtual. This allows the derived class to use the base classes method unless it provides one of it’s own. Surely a similar thing exists for this?

  • 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-10T04:43:34+00:00Added an answer on June 10, 2026 at 4:43 am

    What I would prefer is a way to provide a value in the base class and provide an override in a derived class if needed.

    Properties can be declared virtual, as well:

    public virtual int BaseCost { get { return 0; } }
    public virtual int BaseEnergy { get { return 42; } }
    public virtual int BaseEconomy { get { return 3982; } }
    public virtual int BaseConstruction { get { return 398829; } }
    

    You can then override them when appropriate:

    public override int BaseCost { get { return 2; } }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am doing a simple coin flipping experiment for class that involves flipping a

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.