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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T16:18:56+00:00 2026-05-19T16:18:56+00:00

I’m running into issues with being able to call functions/properties that are unique to

  • 0

I’m running into issues with being able to call functions/properties that are unique to the child class. I’m wondering if there is a way to do do something similar to the following:

public class Creature
{
   public int HealthPoints {get; set;}
   public string Name {get; set;}
   public int AttackValue {get; set;}

   public Creature();
}

public class Magical : Creature
{
   public int Mana {get; set;}
}

So, in this rudimentary example, I’d have a list of Creatures and would need to be able to call from that the Mana property of any given Magical creature.

  • 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-19T16:18:57+00:00Added an answer on May 19, 2026 at 4:18 pm

    The short answer:

    As you have surely seen in other answers, you can access the Mana property of magical creatures as follows:

    foreach (Creature creature in creatures)
    {
        if (creature is Magical magical)
        {
            // ... do something with 'magical.Mana' here...
        }
    }
    

    Going a step further:

    Since we’re talking about polymorphism already, you should know that if (x is SomeType) { .. } can be a code smell. Let’s say you indeed have a foreach loop for processing all your creatures. It might then be better to create a virtual method in the base class, and do whatever you’re doing inside the foreach loop in that virtual method. You would then override the method in your Magical class and thereby get rid of the if statement.

    Let’s do an example. Let’s say, all your creatures get hit and lose some energy. Normal creatures lose health points, while magical creatures lose mana:

    public partial class Creature   // rest of your class is omitted in this example
    {
        public virtual void TakeHit()
        {
            // whatever you had in your original foreach loop, e.g.:
            HealthPoints--;
        }
    }
    
    public partial class Magical : Creature
    {
        public override void TakeHit()
        {
            // ... any special processing for Magical creatures goes here, e.g.:
            Mana--;
            // (you could also call 'base.TakeHit()' in this method.)
        }
    }
    

    Your original foreach loop now becomes much simpler:

    foreach (Creature creature in creatures)
    {                        // note: no more if statement -- The virtual method
        creature.TakeHit();  // invocation now takes care of distinguising
    }                        // different (sub-)types of Creatures!
    

    I would like to recommend a very nice presentation on this very topic to you: Conditionals and Polymorphism.

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want use html5's new tag to play a wav file (currently only supported
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.