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
{
protected 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)