tags:

views:

79

answers:

2

Here's my parent class:

    public abstract class BaseFile
    {
        public string Name { get; set; }
        public string FileType { get; set; }
        public long Size { get; set; }
        public DateTime CreationDate { get; set; }
        public DateTime ModificationDate { get; set; }

        public abstract void GetFileInformation();
        public abstract void GetThumbnail();

    }

And here's the class that's inheriting it:

    public class Picture:BaseFile
    {
        public override void  GetFileInformation(string filePath)
        {
            FileInfo fileInformation = new FileInfo(filePath);
            if (fileInformation.Exists)
            {
                Name = fileInformation.Name;
                FileType = fileInformation.Extension;
                Size = fileInformation.Length;
                CreationDate = fileInformation.CreationTime;
                ModificationDate = fileInformation.LastWriteTime;
            }
        }

        public override void GetThumbnail()
        {

        }
    }

I thought when a method was overridden, I could do what I wanted with it. Any help please? :)

+10  A: 

You cannot change the signature of an overridden method. (Except for covariant return types)

In your code, what would you expect to happen if I run the following:

BaseFile file = new Picture();
file.GetFileInformation();  //Look ma, no parameters!

What would the filePath parameter be?

You should change the parameters of the base and derived methods to be identical.

SLaks
By signature you mean the string filePath I'm giving the GetFileInformation in my picture class? What alternative is there?
Serg
public abstract void GetFileInformation();should be public abstract void GetFileInformation(string filePath);
s_hewitt
You need to have the same parameters in both classes. What are you trying to do?
SLaks
The alternative is to define a new method rather than overriding one, or to change the abstract method's signature so that it has appropriate parameters for what you want it to be used for.
Anon.
What if I want Picture.cs to have a string filePath given to the GetFileInformation(), but in another child class, let's call it, Video.cs I don't want to pass anything to the same method; just call GetFileInformation()?
Serg
That would have to be 2 different methods.
froadie
That would not make sense; see my code example. You should probably create independent methods and not have an `abstract` method in the base class.
SLaks
Something clicked and I now understand why it wouldn't make sense. Thanks a bunch guys. :)
Serg
+1  A: 

You have a parameter in the derived class that's not declared in the parent class. (string filepath in the getFileInformation method)

froadie