views:

75

answers:

4

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...
        }
+5  A: 

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.

codekaizen
+1 for the answer. When you get errors you don't understand, I'd also recommend breaking up the code into multiple lines. You've got quite a number of things on one line, and splitting it up will make it easier to validate at each step. Don't play Code Golf.
kyoryu
+7  A: 

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

Josh Yeager
that should bereturn GetEmployeesArrayList().ToArray( typeof(Employee) );
Mark Arnott
Thanks, Mark. I fixed it.
Josh Yeager
+2  A: 

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();
}
Cylon Cat
A: 

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.

Rune FS