Tony The Lion
Tony The Lion

Reputation: 63190

InvalidCastOperation Exception - can't find what's wrong

I have the following two methods, and the last one throws an InvalidCastOperationException, I cannot see what is wrong.

       public static ArrayList GetEmployeesArrayList()
       {
            ArrayList al = new ArrayList();

           // do some work here...
            return (al);

        }

        public static Employee[] GetEmployeeArray()
        {
            return ((Employee[])GetEmployeesArrayList().ToArray());  <-- Throws invalid cast exception (Cannot cast Employee[] from Object...
        }

Upvotes: 0

Views: 266

Answers (4)

Josh Yeager
Josh Yeager

Reputation: 3793

ArrayList.ToArray() returns an Object[] -- I don't think you can directly cast that to Employee[].

The better way to do this is to use ArrayList.ToArray(Type), which takes a Type parameter specifying what type of object to return. Example:

return GetEmployeesArrayList().ToArray(typeof(Employee));

http://msdn.microsoft.com/en-us/library/system.collections.arraylist.toarray.aspx

Upvotes: 7

codekaizen
codekaizen

Reputation: 27419

You could try to remove the parens:

public static Employee[] GetEmployeeArray()        
{           
    return (Employee[])GetEmployeesArrayList().ToArray(); 
}

However, the dot operator has higher precedence over the cast, so unless this is a typo, the above won't work.

In that case, it would appear that all the elements you add to the ArrayList are not "Employee" types.

Side note: Why not just use a List<Employee>?

EDIT: Josh's answer is right. I didn't see at first that ArrayList.ToArray creates an Object[], instead of just casting to Object[]. You can't cast this to an Employee[] unless you call Array.ConvertAll<TInput, TOutput> or use the type specific overload as Josh correctly pointed out.

Upvotes: 5

Rune FS
Rune FS

Reputation: 21742

ArrayLists are not typed so when you call ToArray() the ArrayList does not check if every object is of a given type it simply returns an object[]. it might not be castable to Employee[].

However you can have ArrayList enforcing that every object in Employee the array is an Employee and then return the resulting object[] to Employee[]

  public static Employee[] GetEmployeeArray()
            {
                return ((Employee[])GetEmployeesArrayList().ToArray(typeof(Employee));
            }

eventhough the return type is object[] it is now actually a Employee[] being returned and that can be casted to Employee[] because that's exactly what it is.

Upvotes: 0

Cylon Cat
Cylon Cat

Reputation: 7201

I've pretty much abandoned all use of ArrayList, but I'll guess that the ArrayList, as an object, can't be cast to an array of Employees, even if you have nothing but Employee objects in your array.

This is a textbook case for generics. Use a list of Employee objects, and you should be clear. List supports a ToArray function, if you need to use an array.

public static List<Employee> GetEmployeeList()
{
    List<Employee> EmpList = new List<Employee>();
    ... do stuff
    return EmpList;
}

public static Employee[] GetEmployeeArray()
{
    return GetEmployeeList().ToArray();
}

Upvotes: 2

Related Questions