C#ArrayList-对象
我的C#计划有三门课。 获取以下错误: 无法将类型“object”隐式转换为 “注册员工。员工”。存在一个显式转换(您是吗 缺少转换?)C:\Users\x64\Documents\Visual Studio 2010\Project\Register Employee\Register Employee\EmployeeList.cs 20注册员工 我知道问题是什么。您必须返回正确类型的对象,但我不知道如何解决它。 我有一个班级雇员,一个班级雇员名单,里面有雇员和主程序C#ArrayList-对象,c#,C#,我的C#计划有三门课。 获取以下错误: 无法将类型“object”隐式转换为 “注册员工。员工”。存在一个显式转换(您是吗 缺少转换?)C:\Users\x64\Documents\Visual Studio 2010\Project\Register Employee\Register Employee\EmployeeList.cs 20注册员工 我知道问题是什么。您必须返回正确类型的对象,但我不知道如何解决它。 我有一个班级雇员,一个班级雇员名单,里面有雇员和主程序 namespace R
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; }
}
}
namespace Register\u员工
{
班级雇员名单
{
ArrayList=新建ArrayList();
公共无效添加员工(员工a)
{
本.列表.添加(a);
}
公共雇员GetEmployee(int索引)
{
var e=list[Index];正如错误消息所说,您需要显式强制转换:
Employee e = (Employee)list[Index];
另外,您可以使用列表来代替ArrayList。只需使用
public Employee GetEmployee(int Index)
{
var e = list[Index]; // should work, e will be of type Object
return (Employee) e; // <<<<<The problems
}
public Employee GetEmployee(int索引)
{
var e=list[Index];//如果有效,e将是Object类型
return(Employee)e;//您将需要键入castobject
toEmployee
object
public Employee GetEmployee(int Index)
{
var e = list[Index];
return (Employee)e;
}
.Net framework 2.0或更高版本有一个通用类列表
,您可以使用它来代替ArrayList
。表示可通过索引访问的对象的强类型列表。请检查
那么
class EmployeeList
{
List<Employee> list = new List<Employee>();
//Rest of your code
}
class员工列表
{
列表=新列表();
//代码的其余部分
}
希望这对您有所帮助。将对象强制转换为员工对象。如果您有选择,请使用列表。您可以通过几种方法解决此问题
首先,如果某个对象存储为对象,但实际上是派生类型,则始终可以将其强制转换为适当的类型
public Employee GetEmployee(int Index)
{
var e = list[Index];
return (Employee)e;
}
但是,这样做会降低类型安全性。更好的解决方案是使用泛型
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
}
}
class员工列表
{
列表=新列表();
公共无效添加员工(员工a)
{
本.列表.添加(a);
}
公共雇员GetEmployee(int索引)
{
var e=列表[索引];
返回e;//不需要强制转换
}
}
在这种情况下,您知道有一个支持类型安全性的列表(即您知道其中的所有内容都是雇员),并且将实际执行数组列表。只需将对象强制转换为雇员
改变这个
var e = list[Index]; <<<<<The problems
var e=list[Index];这里的其他答案建议或暗示您应该使用
List<Employee>
列表
而不是EmployeeList类中的ArrayList。从您的示例来看,EmployeeList似乎只是ArrayList周围的一个强类型包装器。如果是这样,您可以完全摆脱EmployeeList并使用
List<Employee>
列表
相反。当列表更具描述性和类型安全性时,为什么要使用ArrayList
呢?出于兴趣,为什么还要使用非泛型集合?嗨!我对编程非常陌生,我不知道你说的是什么意思,即非泛型集合。“如果你的…2.0或更高版本”:注意var
+1,它讨论了通用列表的好处,而不仅仅是建议使用它。再次遇到同样的问题,无法理解它知道公共int count(){int p=0;int counter=0;var e=new Employee();while(p==0){e=list[counter];}}@MårtenCederholm有什么问题?您的注释中的代码片段是一个基本上不起任何作用的无限循环。它应该做什么?抱歉,list EmployeeList=new list()@Mårtentederholm以及该语句的问题是什么?也许您应该编辑您的问题以发布代码的当前状态并描述当前问题。或者可能需要一个新问题。
List<Employee>
List<Employee>