-1

I'm fairly novice at C# and have searched on this topic here. I think I'm missing something fundamental, so apologies if this is a dumb question.

I have two objects FullAlbum and Album. FullAlbum inherits from Album. I'm trying to turn an Album object into a FullAlbum object so I can work with it. I thought this was as easy as 'Downcasting' it from Album to FullAlbum but I can't get it to work, I get a run time error that says it can't convert from one to the other.

I've already got an Album object _album which is populated when I debug. I'm then trying;

FullAlbum _fullAlbum = new FullAlbum();
_fullAlbum = _album as FullAlbum;

I'm expecting this to copy all the properties from _album to FullAlbum. Am I missing the point?

Tim
  • 28,212
  • 8
  • 63
  • 76
Nick S
  • 61
  • 1
  • 1
  • 6
  • 1
    Possible duplicate of [C# Inheritance & Casting](http://stackoverflow.com/questions/4453242/c-sharp-inheritance-casting) – Victor Zakharov Apr 12 '16 at 23:00
  • 2
    "*I'm expecting this to copy all the properties from _album to FullAlbum. Am I missing the point?*" Yes. Casting does not copy any properties, it simply changes the compiler's understanding of what datatype a variable holds. – p.s.w.g Apr 12 '16 at 23:03
  • One approach to solving your problem would be to create a FullAlbum constructor which takes in a type of Album, and then setting whatever properties on FullAlbum you need inside the constructor. There are some solutions [here](http://stackoverflow.com/questions/729527/is-it-possible-to-assign-a-base-class-object-to-a-derived-class-reference-with-a) which can help with that. – Mash Apr 12 '16 at 23:09
  • Thanks. I'll look further into the C# Inheritance & Casting question to get a better understanding. – Nick S Apr 13 '16 at 01:08
  • So...to clarify a bit further. Just because the FullAlbum class inherits from Album, doesn't mean I can then magically change the Type of an instance of Album to FullAlbum? I need to effectively 'copy' the instance properties across, converting through a constructor or method? I think I get it, but it wasn't intuitively how I thought it would work. Thanks. – Nick S Apr 13 '16 at 01:23
  • Right, since you want to convert to a more specific type, you need to create your own implementation in order to do the conversion. You could potentially have some properties of FullAlbum that don't exist in Album. If so, how would the program know what the values of those properties are? Those additional properties would not be created nor assigned in the Album class. If you were to do things in reverse (i.e. cast FullAlbum to an Album) this is supported by C# because FullAlbum already has all the properties of an Album and therefore can be easily converted to that type. – Mash Apr 13 '16 at 04:39

2 Answers2

0

You can always cast FullAlbum to Album, i.e. treat a specific object as a more generic type. Another way around is not guaranteed to work. No properties are copied, you just specify how you want the program to look at your variable (under which angle so to speak).

EDIT: It is possible to cast Album to FullAlbum and make it work at compile time, however at runtime it will only work if FullAlbum was actually stored in that Album variable. Otherwise it will throw a runtime error.

Example:

class A { }

class B: A { }

//this will work
var A = new B();
var B = (B)A;

//but not this
var A = new A();
var B = (B)A;
Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
0
FullAlbum _fullAlbum = new FullAlbum();
_fullAlbum = _album as FullAlbum;

This does not do what you expect. The as operator says "if this object is actually a FullAlbum then return me a FullAlbum Pointer to it, otherwise return null'. It is not a copy or convert operation.

You say you want to 'convert a album object to a fullalbum object' You cannot do that by magic, you have to create a FullAlbum object and populate it.

The 'as' operator is just for recovering the fact that this really is already a FullAlbum object that you have a Album pointer to.

pm100
  • 48,078
  • 23
  • 82
  • 145
  • Ok - I think this answers that I'm not trying to do the right thing! I'll just 'populate' my FullAlbum object. – Nick S Apr 13 '16 at 01:08