Invalid cast exception: Unable to cast object of type '' to type ''

userfriendly

Newcomer
Joined
May 3, 2010
Messages
3
hi,

im trying to make a simple search tool for an array read from a .txt file. The .txt file has two types of items, movies and games and looks like this for example:

M0001;DVD;The Hours;Nicole Kidman, Meryl Streep;Stephen Daldry;10
G0004;PlayStation 2;Spy Hunter 2;12

the file is read in a FileReader class which has an if statement specifying whether the item is a movie or a game. The 'Movie' and 'Game' classes are inherited from a class called 'RentalItem'.

In the method im writing that does the actual search i have this and it works fine:

Code:
rentalList = new ArrayList();
            rentalList = FileReader.readRentalItems();

public string QueryMovieItem (string title, string actor, string director)

foreach (RentalItem m in rentalList)
            {
                if (m.Title == title)
                    sb.Append(m.ItemId + ", " + m.Title + ", copies: " + m.Copies + "\n\n");
            }

however the following highlights "Movie" and generates the error "unable to cast object of type 'VideoStore.Game' to 'VideoStore.Movie" :

Code:
foreach (Movie m in rentalList)
            {
                if (m.Director == director)
                    sb.Append(m.ItemId + ", " + m.Title + ", copies: " + m.Copies);
            }

can anyone tell me why this is happening and how i can possibly fix it. thanks in advance :)
 
Last edited by a moderator:
The foreach loop will loop over all items in a collection and assume that they are all the type you specified. You can't use the type as a filter. When the loop comes to something that isn't a Movie it throws an error because it was only expecting Movies.

If you want to filter by type, you need to do this yourself. The foreach loop should specify a valid type for all items in the collection. In this case that would be RentalItem. Then, inside the loop, you can check the type of each object using the is operator (or the as operator).
Code:
foreach (RentalItem r in rentalList) {
    if(r is Movie) {
[COLOR="Green"]        // ...[/COLOR]
    }
}
 
thanks so much for the reply snarfblam :) really appreciate it

i tried that and it does get rid of the error but it still leaves another problem. Sorry, I should have given more detail. The RentalItem class carries the properties title, itemId, and copies. then the Movie:RentalItem class carries the remaining properties specific to movies only these are: type, actors, director.

If i use:

foreach (RentalItem m in rentalList)
{
if (m is Movie)
if (m.Director == director)
....etc

then i get another error because RentalItem doesnt contain a definition for director... thats in Movie. Is there a way around this so that i can access the properties in 'Movie'??
 
Last edited:
You would also need to cast the variable m to the correct type e.g.
Code:
foreach (RentalItem m in rentalList)
{
Movie mov = m as Movie;
if(mov != null){
if (mov.Director == director)
'etc
 
Back
Top