开发者

C# Method Hiding

I have a base class which has a method for moving files to appropriate folders. There are many different files with many different naming schemes. The moving and folder creation is the same for every file, but determining the date is different because of the differing file names. I am trying to do this:

public class FileBase
{
   protected FileInfo _source;

   protected string GetMonth()
   {
       // 2/3 Files have the Month in this location
       // So I want this to be used unless a derived class
       // redefines this method.
       return _source.Name.Substring(Source.Name.Length - 15, 2);
   }

   public void MoveFileToProcessedFolder()
   {
      MoveFileToFolder(Properties.Settings.Default.processedFolder + GetMonth);
   }

   private void MoveFileToFolder(string destination)
   {
       ....
   }
}

public class FooFile : FileBase
{
    p开发者_运维知识库rotected new string GetMonth()
    {
        return _source.Name.Substring(Source.Name.Length - 9, 2);
    }
}

public class Program
{
    FooFile x = new FooFile("c:\Some\File\Location_20110308.txt");
    x.MoveFileToProcessedFolder();
}

The problem is that this code results in the base class version of 'GetMonth' being invoked inside the 'MoveFileToProcessedFolder' method. I thought that with the 'new' keyword, this would hide the original implementation and allow the derived implementation to take over. This is not what is happening. Obviously I'm not understanding the purpose of new in this case, can anyone out there help me understand this?

Thanks.


mark the methods as virtual then override them in your derived classes. New allows you to change the signature of the item so if base class has method named void DoWork() you can declare int DoWork() in your derived class by using new keyword. This solves the implicit calls but you can still explicitly call the base class method.

Use virtual (base) and override (derived)


What you really want is to make the base class's method virtual and then override it in the subclass.

public class BaseClass {
    public virtual int Foo() {
        return 1;
    }
}

public class SubClass : BaseClass {
    public override int Foo() {
        return 42;
    }
}


It will only hide when referenced directly by the type that is hiding the method. But since you are calling the implementation from the base class it is deferring to the method defined there.

In your case it sounds like you want virtual implementations rather then method hiding.

public class FileBase
{
   protected FileInfo _source;

   protected virtual string GetMonth()
   {
       // 2/3 Files have the Month in this location
       // So I want this to be used unless a derived class
       // redefines this method.
       return _source.Name.Substring(Source.Name.Length - 15, 2);
   }

   public void MoveFileToProcessedFolder()
   {
      MoveFileToFolder(Properties.Settings.Default.processedFolder + GetMonth());
   }

   private void MoveFileToFolder(string destination)
   {
       ....
   }
}

public class FooFile : FileBase
{
    protected override string GetMonth()
    {
        return _source.Name.Substring(Source.Name.Length - 9, 2);
    }
}

public class Program
{
    FooFile x = new FooFile("c:\Some\File\Location_20110308.txt");
    x.MoveFileToProcessedFolder();
}


In this case you need to use Virtual in the base class and Override in the derived class. It works the way you are expecting using `new' if you do as below.

class Program
    {
        static void Main(string[] args)
        {
            FileBase fb = new FileBase();
            Console.WriteLine(fb.GetMonth());

            FooFile ff = new FooFile();
            Console.WriteLine(ff.GetMonth());

            Console.ReadLine();

        }
    }

   public class FileBase
   {
       public string GetMonth()
       {
           return "FileBase::GetMonth()";
       }

    }

    public class FooFile : FileBase
    {
        public new string GetMonth() // Hides the base method
       {
           return "FooFile::GetMonth()";
       }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜