3

I have three classes in my C# program. Get the following error:

Cannot implicitly convert type 'object' to 'Register_Employee.Employee'. An explicit conversion exists (are you missing a cast?) C:\Users\x64\Documents\Visual Studio 2010\Projects\Register Employee\Register Employee\EmployeeList.cs 20 20 Register Employee

I know what the problem is. You have to return the correct type of object, but I don't know how to solve it. I have a class Employee, a class EmployeeList which holds employees and the main program.

namespace Register_Employee
{
    class EmployeeList
    {
        ArrayList list = new ArrayList();

       public void addEmployee(Employee a)
       {
           this.list.Add(a);
       }

        public Employee GetEmployee(int Index)
        {
            var e = list[Index]; <<<<<The problems
            return e;  <<<<<The problems
        }

    }
}

namespace Register_Employee
{
    class Employee
    {

        public Employee(String iD, String firstName, String lastName)
        {
            this.ID = iD;
            this.FirstName = firstName;
            this.LastName = lastName;
        }

        public String ID { get; set; }
        public String FirstName { get; set; }
        public String LastName { get; set; }


    }
}

Thanks in advance

3
  • 2
    Why are you using ArrayList when List<Employee> would be more descriptive and type safe? Commented Jan 20, 2012 at 12:33
  • 3
    Out of interest, why are you still using non-generic collections? Commented Jan 20, 2012 at 12:33
  • Hi! I very new to programming and I don't know what you mean by that, non-generic collections that is. Commented Jan 21, 2012 at 1:50

7 Answers 7

4

As the error message says, you need to explicitly cast:

Employee e = (Employee)list[Index];

Also, you could use a List<Employee> instead of ArrayList.

Sign up to request clarification or add additional context in comments.

Comments

3

You can solve the problem in a couple of ways.

First if a thing is stored as an object but is actually a derived type you can always cast it to the appropriate type.

public Employee GetEmployee(int Index)
    {
        var e = list[Index]; 
        return (Employee)e; 
    }

However you loose type safety in doing so. A better solution is to use generics

class EmployeeList
{
   List<Employee> list = new List<Employee>();

   public void addEmployee(Employee a)
   {
       this.list.Add(a);
   }

    public Employee GetEmployee(int Index)
    {
        var e = list[Index]; 
        return e;  //No cast needed 
    }

}

In this case you know have a list which supports type safety (ie you know everything in it is a Employee ) and will actually out perform the array list.

1 Comment

+1 for discussing the benefits of the generic List rather than just suggesting its use.
3

You will need to type cast object to Employee object

public Employee GetEmployee(int Index)
{
    var e = list[Index]; 
    return (Employee)e;  
}

.Net framework 2.0 or later has a generic class List which you can use instead of ArrayList. Represents a strongly typed list of objects that can be accessed by index. Check on MSDN.

In that case

class EmployeeList
{
    List<Employee> list = new List<Employee>();
    //Rest of your code
}

Hope this helps you.

1 Comment

"if your ... 2.0 or later" : notice the var.
2

Just use

public Employee GetEmployee(int Index)
{
  var e = list[Index];  // should work, e will be of type Object
  return (Employee) e; // <<<<<The problems
}

And consider using List<Employee> to have a more 'type-safe' collection.

class EmployeeList
{
    //ArrayList list = new ArrayList();
    List<Employee> list = new List<Employee>();
    ...
}

Comments

2

Cast the object to the Employee object. Use List if you have the option.

Comments

1

Just cast the object as an Employee Change this

var e = list[Index]; <<<<<The problems

To this

var e = (Employee)list[Index]; <<<<<The problems

like a couple of guys said, you should definitely NOT use a generic Collection/List/IEnumerable

Comments

1

The other answers here suggest or imply that you should use

List<Employee>

rather than ArrayList in your EmployeeList class. From your sample, it seems that EmployeeList is just a strongly-typed wrapper around ArrayList. If that's the case, you can get rid of EmployeeList entirely and use

List<Employee>

instead.

4 Comments

Got the same problem again, can't understand it know public int count() { int p = 0; int counter = 0; var e = new Employee(); while(p == 0) { e = list[counter]; } }
@MårtenCederholm what is the problem? The code fragment in your comment is an infinite loop that does essentially nothing. What is it supposed to do?
Sorry, List<Employee> EmployeeList = new List<Employee>();
@MårtenCederholm And what's the problem with that statement? Perhaps you should edit your question to post the current state of your code and describe the current problem. Or perhaps a new question is warranted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.