views:

215

answers:

6

Again disclaimer disclaimer still learning C# and OOP generally so I hope you'll be patient with me :)

I am currently working with a CMS that has a class called FileVersion which basically contains a list of properties pertaining to a file such as filename, filetype, size in bytes, id, date uploaded, is-latest-version, etc.

A list of FileVersions is contained within a File which has it's own unique ID. When you want to download a particular file from the CMS, the URL is built using something like the following:

string URL = "/files/"+file.id.toString()+"/"+file.fileVersion.Last().filename;

Now for a particular control I'm working on (dealing specifically with files that are documents), it makes sense to be able to store the URL along with all the other FileVersion information for later use. So what I've decided to do is create my own class called DocumentVersion which extends FileVersion. This is what it looks like:

public partial class DocumentVersion : FileVersion
{
    public DocumentVersion() : base() { }
    public string link;
}

Now I should note, that it appears that I don't need to implement an interface here - but don't take that as gospel, that's why I'm here.

However when I try to cast my FileVersion as a DocumentVersion like so:

DocumentVersion dv = ((DocumentVersion)fileversion);

I get the following exception:

Unable to cast object of type 'Foo.CMS.FileVersion' to type 'CoA.DocumentVersion'.

My inkling is that it's because I'm trying to extend a class in a different namespace from where the original resides, but like I said, OOP is relatively new to me so I could be wrong.

Thanks in advance for your help. This community has been so valuable! I just hope once I'm more skilled up I can give some back :).

+1  A: 

You can't make father (FileVersion) to act like son (DocumentVersion).
Only vica versa => you can make son as father.

(FileVersion)documentVersion //valid
(DocumentVersion)fileVersion //invalid (because fileVersion can't know about stuff that derived type has)

That's OOP basics.

Arnis L.
+5  A: 

Are you trying to downcast ? i.e. take an existing FileVersion and declare it to be a DocumentVersion ?

If so, you can't do that, since what you have is nothing more than a FileVersion.

Instead you need to create a new DocumentVersion object. Your DocumentVersion constructor will take the arguments required for a FileVersion and then call the FileVersion constructor via base().

Note that if you have an existing FileVersion object, you may want an object to wrap this, rather than have an object deriving from it. e.g. your DocumentVersion wouldn't derive from FileVersion, but contain a private reference to a FileVersion, plus additional data as required. That may be more appropriate in this scenario.

e.g. (in OO terms this is composition)

public class DocumentVersion {
   private FileVersion fv;
   private String url;
   public DocumentVersion(FileVersion fv, String url) {
      this.fv = fv;
      this.url = url;
   }
}

See here for more info on composition.

Brian Agnew
Aha! Looks like I have the cart before the horse! It sounds very much like what I want to do is wrap FileVersion rather than extend it. I will certainly google what you've suggested, but if it's a simple couple of lines, would you mind showing me how to do this?
Iain Fraser
See above. I'm a Java guy, so my C# is a little rusty. But the above should give you the idea.
Brian Agnew
Wrapping: Your DocumentVersion class would include an instance variable pointing to the FileVersion and additional field that you want to store. The problem is that you need to take care of storing this DocumentVersion yourself, you cannot just *attach* it to the existing FileVersion or File. So, you'd need to create your own `Dictionary<File, DocumentVersion>` to store the additional information. Of course, once you do that, the FileVersion reference in DocumentVersion is not really needed anymore, since it's accessible through File anyway.
Heinzi
+1  A: 
DocumentVersion dv = null;

if (fileversion is DocumentVersion)
{
    dv = fileversion as DocumentVersion;
}
bniwredyc
+4  A: 

You cannot cast your FileVersion to DocumentVersion because your fileversion variable contains a FileVersion, not a DocumentVersion. Inheritance means the following:

  • Every DocumentVersion is a FileVersion, but
  • Not every FileVersion is a DocumentVersion!

If the object was created as a FileVersion, it's just a FileVersion. Period. If it was created as a DocumentVersion, you can use it everywhere where you would use a FileVersion (see rule #1 above), and you can use the DocumentVersion features of it.

So, when creating the FileVersion object, you need to create a DocumentVersion instead (if you have control over that part of the code), then the cast will work. Those DocumentVersions could be stored in the same list as the FileVersions, since every DocumentVersion is also a FileVersion.


EDIT: Since the above two rules are so vital to understanding the OO principle, let me illustrate them with an example: DocumentVersion = dog and FileVersion = animal. Then the above rules would be: (1) Every dog is an animal, but (2) not every animal is a dog. So, you can create a list of animals, store all kinds of animals in there (dogs, cats, things that are just "animal", ...), but you cannot cast a variable of type animal to a dog, unless what is in there has been created as a dog (or as a poodle (class poodle : dog), which is a dog by rule (1)).

In OO terms: If you store a dog in a variable of type animal, then the static type of the object is "animal", and the dynamic type is "dog". You can only cast an element to some type T if its dynamic type is T or a subtype thereof.

Heinzi
+1  A: 

(As most answers have said, you can't cast back up - FileVersion has no idea what a DocumentVersion is).

One way to get around it is to copy the fields in the constructor of DocumentVersion

public partial class DocumentVersion : FileVersion
{
    public DocumentVersion() : base() { }

    public DocumentVersion(FileVersion version) : this()
    {
     this.id = version.id;
     // etc.
    }

    public string Link { get;set; }
}

Or create a static method on DocumentVersion that returns a DocumentVersion from the FileVersion you provide it, e.g:

public static void New(FileVersion version)
{
    this.id = version.id;
    // etc.
}

Another technique mentioned, is composition. You store the FileVersion as a property in DocumentVersion

public class DocumentVersion
{
    public FileVersion FileVersion { get;set; }
    public string Link { get;set; }
}
Chris S
A: 

Since you cannot convert a FileVersion to a DocumentVersion and all you need to do is add a method to get the URL, you could create an extension method that takes a FileVersion:

public static string GetLink(this FileVersion fileVersion) {
  return "/files/"+fileVersion.id.ToString()+"/"+fileVersion.FileVersion.Last().Filename
}

Note : this only works if you are using C# 3.0.

Julien Poulin