Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Will calling a new method/property work if called on a "base" abstract object

Will calling a new method/property work if called on a "base" abstract object

Scheduled Pinned Locked Moved C#
question
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pr1mem0ver
    wrote on last edited by
    #1

    I am working with SharpCompress and creating a parser that creates objects mimicking real folder structures to use in various browsable controls. Since the IEntry and IArchiveEntry interfaces in SharpCompress have all the makings of an "info" class, I have created two custom Info classes derived from System.IO.FileSystemInfo. Because most of the properties are NOT virtual, I have to use the new keyword to override them so that I can grab the info from the Entry object instead of the file system. I want to use the base class FileSystemInfo to supply much of the data for my virtual file system object class. However, I am not sure if using the FileSystemInfo object will work with the derived classes that hide the original properties. Check the relevant part of my code below:

    public class IOObjectNode : TreeNode;
    {
    FileSystemInfo info;
    FileInfo file;
    DirectoryInfo folder;
    ArchiveDirectoryInfo virtualFolder;
    ArchiveFileInfo virtualFile;

    IEntry entry;
    public string FileName { get { return info.Name; } }
    public string Path { get { return System.IO.Path.GetDirectoryName(info.FullName); } }
    public string FullName { get { return info.FullName; } }
    public DateTime LastWriteTime { get { return info.LastWriteTime; } }
    public DateTime CreationTime { get { return info.CreationTime; } }
    public long Size { get { if (file != null) return file.Length; else return 0; } }
    public long Length { get { return Size; } }
    public IOObjectNode() { }
    public void Populate(IEntry entry) { SharpEntry = entry; }
    public void Populate(FileSystemInfo info) { ShellInfo = info; }
    
    public FileSystemInfo ShellInfo
    {
    	get { return info; }
    	protected set
    	{
    		info = value;
    		file = value as FileInfo;
    		folder = value as DirectoryInfo;
    		virtualFolder = value as ArchiveDirectoryInfo;
    		virtualFile = value as ArchiveFileInfo;
    
    		UpdateResources();
    	}
    }
    
    public IEntry SharpEntry
    {
    	get { return entry; }
    	protected set
    	{
    		entry = value;
    		UpdateResources();
    	}
    }
    

    }

    Notice my use of "info" in some of the properties above. Will this work with my custom class instances when info is the base class?

    P 1 Reply Last reply
    0
    • P pr1mem0ver

      I am working with SharpCompress and creating a parser that creates objects mimicking real folder structures to use in various browsable controls. Since the IEntry and IArchiveEntry interfaces in SharpCompress have all the makings of an "info" class, I have created two custom Info classes derived from System.IO.FileSystemInfo. Because most of the properties are NOT virtual, I have to use the new keyword to override them so that I can grab the info from the Entry object instead of the file system. I want to use the base class FileSystemInfo to supply much of the data for my virtual file system object class. However, I am not sure if using the FileSystemInfo object will work with the derived classes that hide the original properties. Check the relevant part of my code below:

      public class IOObjectNode : TreeNode;
      {
      FileSystemInfo info;
      FileInfo file;
      DirectoryInfo folder;
      ArchiveDirectoryInfo virtualFolder;
      ArchiveFileInfo virtualFile;

      IEntry entry;
      public string FileName { get { return info.Name; } }
      public string Path { get { return System.IO.Path.GetDirectoryName(info.FullName); } }
      public string FullName { get { return info.FullName; } }
      public DateTime LastWriteTime { get { return info.LastWriteTime; } }
      public DateTime CreationTime { get { return info.CreationTime; } }
      public long Size { get { if (file != null) return file.Length; else return 0; } }
      public long Length { get { return Size; } }
      public IOObjectNode() { }
      public void Populate(IEntry entry) { SharpEntry = entry; }
      public void Populate(FileSystemInfo info) { ShellInfo = info; }
      
      public FileSystemInfo ShellInfo
      {
      	get { return info; }
      	protected set
      	{
      		info = value;
      		file = value as FileInfo;
      		folder = value as DirectoryInfo;
      		virtualFolder = value as ArchiveDirectoryInfo;
      		virtualFile = value as ArchiveFileInfo;
      
      		UpdateResources();
      	}
      }
      
      public IEntry SharpEntry
      {
      	get { return entry; }
      	protected set
      	{
      		entry = value;
      		UpdateResources();
      	}
      }
      

      }

      Notice my use of "info" in some of the properties above. Will this work with my custom class instances when info is the base class?

      P Offline
      P Offline
      pr1mem0ver
      wrote on last edited by
      #2

      Nevermind. Found the answer myself with a few test classes:

      public class CoreClass
      {
      public int Value { get { return 1; } }
      }

      public class ClassA : CoreClass
      {
      public new int Value { get { return 2; } }
      }

      public class ClassB : CoreClass
      {
      public new int Value { get { return 3; } }
      }

      public class ClassC : CoreClass
      {
      public new int Value { get { return 4; } }
      }

      public class ClassD : CoreClass
      {
      public new int Value { get { return 5; } }
      }

      public class ContainerClass
      {
      public CoreClass CoreObject { get; set; }
      public int CoreValue { get { return CoreObject.Value; } }

      public ContainerClass(CoreClass instance) { CoreObject = instance; }
      

      }

      static void Main(string[] args)
      {
      ContainerClass a = new ContainerClass(new ClassA());
      ContainerClass b = new ContainerClass(new ClassB());
      ContainerClass c = new ContainerClass(new ClassC());
      ContainerClass d = new ContainerClass(new ClassD());

      int av = a.CoreValue;
      int bv = b.CoreValue;
      int cv = c.CoreValue;
      int dv = d.CoreValue;	
      

      }

      Bummer... all the int values in Main were 1. So looks like it calls the base class method and not the derived one. Looks like I will need to try a different approach.

      OriginalGriffO 1 Reply Last reply
      0
      • P pr1mem0ver

        Nevermind. Found the answer myself with a few test classes:

        public class CoreClass
        {
        public int Value { get { return 1; } }
        }

        public class ClassA : CoreClass
        {
        public new int Value { get { return 2; } }
        }

        public class ClassB : CoreClass
        {
        public new int Value { get { return 3; } }
        }

        public class ClassC : CoreClass
        {
        public new int Value { get { return 4; } }
        }

        public class ClassD : CoreClass
        {
        public new int Value { get { return 5; } }
        }

        public class ContainerClass
        {
        public CoreClass CoreObject { get; set; }
        public int CoreValue { get { return CoreObject.Value; } }

        public ContainerClass(CoreClass instance) { CoreObject = instance; }
        

        }

        static void Main(string[] args)
        {
        ContainerClass a = new ContainerClass(new ClassA());
        ContainerClass b = new ContainerClass(new ClassB());
        ContainerClass c = new ContainerClass(new ClassC());
        ContainerClass d = new ContainerClass(new ClassD());

        int av = a.CoreValue;
        int bv = b.CoreValue;
        int cv = c.CoreValue;
        int dv = d.CoreValue;	
        

        }

        Bummer... all the int values in Main were 1. So looks like it calls the base class method and not the derived one. Looks like I will need to try a different approach.

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        Well yes - that is what the new keyword is there to do: Knowing When to Use Override and New Keywords - C# Programming Guide | Microsoft Docs[^] If you want to extend behaviour, you override properties / methods.

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        P 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          Well yes - that is what the new keyword is there to do: Knowing When to Use Override and New Keywords - C# Programming Guide | Microsoft Docs[^] If you want to extend behaviour, you override properties / methods.

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

          P Offline
          P Offline
          pr1mem0ver
          wrote on last edited by
          #4

          Well you can't override non-virtual/abstract methods or properties and I can't change Microsoft's base classes. Pretty annoying because it would be a simple solution to a now a more complex problem. I am guessing I will have to make a wrapper.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups