Reputation: 87
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
Upvotes: 3
Views: 5501
Reputation: 43046
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.
Upvotes: 1
Reputation: 3624
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
Upvotes: 1
Reputation: 9680
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.
Upvotes: 3
Reputation: 25505
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.
Upvotes: 3
Reputation: 162
Cast the object to the Employee
object. Use List if you have the option.
Upvotes: 2
Reputation: 273244
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>();
...
}
Upvotes: 2
Reputation: 2972
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.
Upvotes: 4