【发布时间】:2019-06-27 02:34:00
【问题描述】:
我是 .Net 框架的新手,我收到一条错误消息
“List(Of AdminSetEmployeeParams)”类型的值无法转换为 'AdminSetEmployeeParams'"
Dim SetNewEmployee As New List(Of AdminSetEmployeeParams)
SetNewEmployee.Add(New AdminSetEmployeeParams With {
.departmentId = ddlDept.SelectedValue,
.familyName = txtLastOrSurname.Text,
.firstName = txtFirstOrGivenName.Text,
.secondName = txtSecondName.Text,
.contactPhone = txtPhone.Text,
.user = ""})
SetNewEmployee = EmployeeAPIService.AdminSetEmployee(SetNewEmployee).Result
我将如何解决此错误?
更新:
System.Threading.Tasks
Public Class Task(Of TResult)
Public ReadOnly Property Result As TResult
【问题讨论】:
-
暂时忽略错误,代码实际上并没有什么意义。您声明变量,创建一个新的
List并将其分配给该变量,将一个项目添加到List,然后通过将其他内容分配给该变量来完全丢弃该List。最后一行是否分配给了错误的变量?如果不是,那么创建一个新的List并在其中添加一个项目,如果你只是想扔掉它有什么意义呢? -
至于错误消息,它告诉您
EmployeeAPIService.AdminSetEmployee(SetNewEmployee).Result是AdminSetEmployeeParams类型,但您将其分配给List(Of AdminSetEmployeeParams)类型的变量。这就像试图将鸡蛋放在需要鸡蛋盒的地方。这就是为什么我想知道分配给该变量是否是您真正想要做的。实际上应该是SetNewEmployee.Add(EmployeeAPIService.AdminSetEmployee(SetNewEmployee).Result)?你应该分配给不同的变量吗?试着解释你真正想要完成的事情。 -
另外,如果这是一个运行时异常,那么这意味着您必须拥有
Option Strict Off,这很糟糕。您应该在项目属性和 IDE 选项中设置Option Strict On,因此将来默认为On。这样的问题将在设计时而不是运行时被标记。 -
回复您的第二条评论...我正在尝试将集合传递给由其他人创建的 API(这不是最好的设计,但现在我需要将数据传递给 API)
-
在您的第二条评论中实现代码时,我收到此错误“List(Of AdminSetEmployeeParams)”类型的值无法转换为“AdminSetEmployeeParams”
标签: vb.net collections