Question

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

Was it helpful?

Solution

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;

OTHER TIPS

"'app1.AudioDirectory.file_entries': type must be 'app1.FileEntry[]' to match overridden member 'app1.Directory.file_entries'"

Means that you need to return the same type that you override from the base class:

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

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

If you have other problems, please provide all of your code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top