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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:59:11+00:00 2026-06-11T22:59:11+00:00

Since this is my first StackOverflow post I’d like to briefly say a huge

  • 0

Since this is my first StackOverflow post I’d like to briefly say a huge thanks to those who manage the site and to those who actively offer assistance to others. StackOverflow has been a highly useful resource in solving C# code challenges.

Class description: In a MS VisualStudio 2010 C# .NET console app I have the following base/derived hierarchy:

  • Entry -> DirEntry
  • Entry -> FileEntry -> AudioFileEntry -> OutputFileEntry

…the latter three of which are respectively members in the following base/derived hierarchy:

  • Directory -> AudioDirectory -> OutputDirectory

…such that:

  • Directory has member public FileEntry[] file_entries;
  • AudioDirectory has member public AudioFileEntry[] file_entries;
  • OutputDirectory has member public OutputFileEntry[] file_entries;

Base class Entry is declared in the following manner:

public class Entry : IComparable
{
    public string name;
    public int entryID;
    public int parentID;
    //(other code omitted)
}

Directory also has member public DirEntry[] dir_entries; which presents no problem, but please note DirEntry isn’t overridden any deeper than Directory (There’s no AudioDirEntry, for example).

Problem: NullReferenceException attempting to read common members within file_entries[n] from within base-class Directory methods or from other classes when the type is AudioDirectory or OutputDirectory.

No pertinent classes, members, or methods are static. I’ve tried using new and override to no avail. Attempting to make file_entries a property:

public class Directory
{
    public virtual FileEntry[] file_entries { get; set; }
}

public class AudioDirectory : Directory
{
    public override AudioFileEntry[] file_entries { get; set; }
    //(other code omitted)
}

…fails with the error, “…'app1.AudioDirectory.file_entries': type must be 'app1.FileEntry[]' to match overridden member 'app1.Directory.file_entries'“

Goal: The ability to reference file_entries in base-class Directory methods, regardless of which derived type the object is. My gut tells me I’m probably overlooking something simple, since the above is essentially what polymorphism is all about, and there should be a way to accomplish this without resorting to the new dynamic type.

Suggestions?

-Turbine

  • 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-11T22:59:12+00:00Added an answer on June 11, 2026 at 10:59 pm

    Your problem is the fact that when you override a property or a method you can’t change the signature, i.e. the types involved. You can ignore this limitation using the new modifier, but you have to be well aware of what this implies (more on this later). In your case, you have to avoid the implicit definition of your properties in the subclasses because this would declare a new variable in the subclass which is different from the one in the base class. I mean that your code would be the same as this:

    public class Directory
    {
        private FileEntry[] _file_entries;
        public virtual FileEntry[] file_entries 
        {
            get { return _file_entries; }
            set{_file_entries = value;} 
        }
    }
    
    public class AudioDirectory
    {
        private AudioFileEntry[] _sub_file_entries;
        public new AudioFileEntry[] file_entries 
        {
            get { return _sub_file_entries; }
            set{_sub_file_entries = value;} 
        }
    }
    

    This means that when you set file_entries in AudioDirectory the variable accessed in the base class is still null.

    A possible solution is this one:

    public class Directory
    {
        private FileEntry[] _file_entries;
        public virtual FileEntry[] file_entries 
        {
            get { return _file_entries; }
            set{_file_entries = value;} 
        }
    }
    
    public class AudioDirectory : Directory
    {
        public new AudioFileEntry[] file_entries
        {
            get { return (AudioFileEntry[])base.file_entries; }
            set { base.file_entries = value; }
        }
    }
    

    This should work, but be careful to the fact that using the new modifier forces a compile time binding, and this means that the property which is executed depends on the type of the declared variables, not on the actual typoe of the instances. For instance in the following code you have a single instance of AudioDirectory, but the first call of file_entries executes the AudioDirectory version, the second the Directory one:

            AudioDirectory ad = new AudioDirectory();
            Directory d = ad;
    
            FileEntry[] fa = ad.file_entries;
            FileEntry[] fb = d.file_entries;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is my first post here on StackOverflow and I'm quite excited about it
Since this is my first question here on stackoverflow I hope my question is
I will start with a quick Hello since it's my first post on stackoverflow.
This is my first post on StackOverflow. This community has provided me with some
This is my first post here in StackOverflow. I am not a newbie to
Hey everyone, this is my first post up on StackOverflow! WOO! Anyhow, onto my
This is my first question and in Stackoverflow although i must say that this
Require a huge help here, since this is affecting our production instance. One of
This is my first post, so please be gentle. I've been playing around with
Heyo. This is my first stack overflow post because I am stumped and not

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.