0

I have 2 classes:

public class Place
{
    public string GoogleReference { get; set; }
    public string GoogleID { get; set; }
    public string Name { get; set; }
    public string Vicinity { get; set; }
}

public class PlaceDetail : Place
{
    public string Url { get; set; }
    public string InternationalPhoneNumber { get; set; }
    public string Website { get; set; }
}

How to cast Place to PlaceDetail? Next code does not work

Place place = new Place();
place.Name = "A";
....
PlaceDetail placeDetail = (PlaceDetail)place;

Unable to cast object of type 'PlacesLibrary.Place' to type 'PlacesLibrary.PlaceDetail'.

Ry-
  • 218,210
  • 55
  • 464
  • 476
Oleg Sh
  • 8,496
  • 17
  • 89
  • 159

2 Answers2

1

Following the object-oriented philosophy, a "PlaceDetail" object IS-A "Place" object, so you do not have to cast!

Place placeDetail = new PlaceDetail();

If you want to "upgrade" your object from Place to PlaceDetail, you can proceed in a variety of ways. For instance, you could build a method in a factory class to do that, and enter in this factory method the parameters you need to specify all "details".

First you must implement a "copy constructor" in PlaceDetail to initialize a PlaceDetail from a Place, ie.:

public PlaceDetail(Place p)
{
    this.GoogleReference = p.GoogleReference;
    this.GoogleID = p.GoogleID;
    this.Name = p.Name;
    this.Vicinity = p.Vicinity;
}

then, you could build your factory method as follows:

static class PlaceDetailFactory
{
    public static PlaceDetail create(Place p, string url, string phone, string site)
    {
        PlaceDetail pd = new PlaceDetail(p);
        pd.Url = url;
        pd.InternationalPhoneNumber = phone;
        pd.Website = site;
        return pd;
    }
}
Guildenstern70
  • 1,715
  • 18
  • 18
0

You can't cast to a subclass.

You can only cast to a superclass:

PlaceDetail placeDetail = new PlaceDetail();
Place place = placeDetail;

Note that this rule only applies to the actual type of the object, not the type of the variable. Once you've cast it to a superclass, you can cast it back to a subclass because the actual type of the object is still the same. However, here the cast needs to be explicit.

So this is perfectly valid:

// casting to a superclass
Place place = new PlaceDetail();

// Here 'place' is of type 'Place', but the actual object referenced by 'place' 
//   is of type 'PlaceDetail', so we can cast it to that
PlaceDetail placeDetail = (PlaceDetail)place;

An example to help:

Vehicle
Truck - subclass of Vehicle
Motorcycle - subclass of Vehicle

So you can say a Truck is a Vehicle. You can't say any Vehicle is a Truck (it could be a Motorcycle, or a generic Vehicle).

To say that a Truck is a Vehicle doesn't change it at all, you're just using a more generic name for it. You can go back to calling it a Truck.

Ok, technically not the best example, since I'm not entirely sure what a generic vehicle will look like, but let's say such a thing existed.

Here are some other examples. And here.

Community
  • 1
  • 1
Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138