Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to cast Base class (data contract) to derived class

[DataContract]
public class SearchCriteria
{
    [DataMember]
    public string CountryID { get; set; }    
}

[DataContract]
public class CitySearchCriteria: SearchCriteria
{
    [DataMember]
    public string CityID { get; set; }
}

I am creating an instance of SearchCriteria in my MVC controller action, and trying to convert it into CitySearchCriteria.

SearchCriteria searchCriteria = new SearchCriteria();
searchCriteria.CountryID = "1";
CitySearchCriteria citySearchCriteria = searchCriteria as CitySearchCriteria;

The "citySearchCriteria" object after the above statement is showing NULL value. I was expecting it to show both properties, CountryID and CityID with CountryID populated, and CityID blank... but it is setting the object to NULL.

What could be the solution here? Has DataContract to do anything with this?

The comments are suggesting, you cannot convert a base to derive: but actually, I have done this successfully in my view, its just not working in controller action:

CitySearchCriteria citySearchCriteria = (CitySearchCriteria)Model.SearchCriteria;

This is converting successfully, so why not the similar thing working in controller action?

like image 725
Nirman Avatar asked May 06 '13 05:05

Nirman


2 Answers

You cannot cast this way!

If you do new you create a new memory object of a certain size. In your case new SearchCriteria() creates a new memory object with enough size to hold one string, nothing more, nothing less.

In your last line you do searchCriteria as CitySearchCriteria trying to cast the object in searchCriteria to a larger type CitySearchCriteria. But it cannot be done. You are trying to 'convert' a memory object which holds 1 string into a memory object that can hold 2 strings. But casting does not convert an new memory object. What would be the value of the new string? It simply looks under water to check if your reference searchCriteria already contains an object of type CitySearchCriteria. In your case: it does not (the object is of type SearchCriteria) and returns null.

So... the next example DOES work (because CitySearchCriteria has already been created). This is also your solution:

SearchCriteria searchCriteria = new CitySearchCriteria(); 
CitySearchCriteria citySearchCriteria = searchCriteria as CitySearchCriteria;

And this does not work (because CitySearchCriteria has NOT already been created). This is your situation:

SearchCriteria searchCriteria = new SearchCriteria();
CitySearchCriteria citySearchCriteria = searchCriteria as CitySearchCriteria;

It is the same thing as the next example.
This does work (because SearchCriteria has already been created):

object o = new SearchCriteria();
SearchCriteria searchCriteria = o as SearchCriteria;

And this does not (because SearchCriteria has NOT already been created)::

object o = new object();
SearchCriteria searchCriteria = o as SearchCriteria;

For the record: I would always use a direct cast, not a cast using as, unless you want to explicitly test if an object is of that type.

like image 75
Martin Mulder Avatar answered Oct 15 '22 22:10

Martin Mulder


Everybody has already (and correctly) told you that you simply can't cast from Base to Derived, but it seems to me that you still don't get the reason why this line works in another chunk of your code:

CitySearchCriteria citySearchCriteria = (CitySearchCriteria)Model.SearchCriteria;

I think that you are a little bit confused about what the "Type" of an instance is. You didn't post the definition of Model, but I think that you have something like this:

public SearchCriteria SearchCriteria;

This doesn't mean that SearchCriteria always contains instances of SearchCriteria, but only that it contains instances of types that can be cast to SearchCriteria. In your case it can contain instances of SearchCriteria or of CitySearchCriteria. I suppose that somewhere in your code you will find something like:

Model.SearchCriteria = new CitySearchCriteria();

and this is what allows yor cast to be executed correctly. You can see that the instance is indeed a CitySearchCriteria (and not simply an instance of SearchCriteria) executing this code just before the cast:

MessageBox.Show(Model.SearchCriteria.GetType().FullName);

To understand better you could try to modify the value in SearchCriteria just before your working cast as shown below only to find out that the cast won't work anymore:

Model.SearchCriteria = new SearchCriteria();
MessageBox.Show(Model.SearchCriteria.GetType().FullName);
CitySearchCriteria citySearchCriteria = (CitySearchCriteria)Model.SearchCriteria;
like image 45
Francesco Baruchelli Avatar answered Oct 15 '22 22:10

Francesco Baruchelli