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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:31:40+00:00 2026-05-19T17:31:40+00:00

The follow is bad code. Please help me achieve what I’m trying to achieve,

  • 0

The follow is bad code. Please help me achieve what I’m trying to achieve, but with good code.
I want to have an object array that is derived from a base. I want to be able to use that array in BaseClass when DoStuff() runs. I know that I cannot downcast, that’s not what i’m looking to do here. I just want to be able to set Fields in the derived class, and use Fields in the base DoStuff().

public class BaseObject
{

}

public class DerivedObject : BaseObject
{

}

public class BaseClass
{
    public BaseObject[] Objects;

    public virtual void DoStuff()
    {
        // use the Objects
    }
}

public class DerivedClass : BaseClass
{
    public override DerivedObject[] Objects;

    public override void DoStuff()
    {
        // Do stuff unique to the derived.

        base.DoStuff();
    }
}
  • 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-19T17:31:41+00:00Added an answer on May 19, 2026 at 5:31 pm

    You are looking for generics?

    public class BaseClass<T>
    where T : BaseObject
    {
        public T[] Objects;
    
        public virtual void DoStuff()
        {
            // use the Objects
        }
    }
    
    public class DerivedClass : BaseClass<DerivedObject>
    {
        public override void DoStuff()
        {
            // Do stuff unique to the derived.
    
            base.DoStuff();
        }
    }
    

    In the above code field of T type (generic or parameter type) is declared, T is constrained to be BaseObject or class inherited from BaseObject, when DerivedClass is declared DerivedObject is specified in place of T, so in DerivedClass Objects field can be used as DerivedObject[].

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

Sidebar

Related Questions

Follow up to this question . I have the following code: string[] names =
I have written code as follows. Problem is that I can't remove Event.COMPLETE event
So I am trying to follow this tutorial to debug my code. I tell
Let us say we have some code that looks like below: @interface SomeClass :
I have often heard that using break s in Java is considered bad practice,
I have this line in my code: $item=$page->items; $page is an array, and its
First, sorry for my bad english. I have an EF entity that looks like:
In follow up to this question , it appears that some numbers cannot be
I follow this rule but some of my colleagues disagree with it and argue
I follow the steps in the customdialog example in the documentation, but I get

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.