【问题标题】:Error when creating a List object in VB.Net在 VB.Net 中创建 List 对象时出错
【发布时间】: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).ResultAdminSetEmployeeParams 类型,但您将其分配给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


【解决方案1】:

可能是这样,但很难确定:

Dim newEmployee As New AdminSetEmployeeParams

newEmployee = New AdminSetEmployeeParams With {
                .departmentId = ddlDept.SelectedValue,
                .familyName = txtLastOrSurname.Text,
                .firstName = txtFirstOrGivenName.Text,
                .secondName = txtSecondName.Text,
                .contactPhone = txtPhone.Text,
                .user = ""})

Dim newEmployeeResult As List(Of AdminSetEmployeeParams) = EmployeeAPIService.AdminSetEmployee(newEmployee).Result

我不确定您的 API 为什么会在其结果中返回员工列表(我假设它确实是因为您没有说您有一条错误消息抱怨将结果分配给列表(of. ..) ) 但是只有当 API 调用需要一个新员工并且您将新员工列表交给它时,才会合理地发生给出的错误

如果不成功,试试最后一行

Dim newEmployeeResult As AdminSetEmployeeParams = EmployeeAPIService.AdminSetEmployee(newEmployee).Result

如果这不起作用,请编辑您的问题以提供有关 AdminSetEmployee 采用哪种参数以及 .Result 是哪种对象的更多信息

【讨论】:

  • 在你的最后一行中,我在代码行“EmployeeAPIService.AdminSetEmployee(newEmployee).Result”中收到此错误“List(Of AdminSetEmployeeParams)”类型的值无法转换为“AdminSetEmployeeParams”
  • 不幸的是我没有特权格式化 cmets 部分:-(
  • 你可以尝试从最后一行删除As List(Of AdminSetEmployeeParams),让类型推断来做它的事情,前提是API的返回值是强类型的(如果它被声明,这不起作用作为类型 Object--- 或者更确切地说,它给你一个结果,它本身就是 Object 类型的结果,然后你需要对其进行强制转换,但最好还是显式强制转换并保留推断的声明)。
  • @dotNetRookie 那么您的服务返回了什么?单个员工参数或员工参数列表?我猜到了返回值,因为你没有说它会导致错误(但现在你确实说它会导致错误)
  • 除了成功消息之外,它没有返回任何内容。 API 是将员工记录保存到数据库中。我一次传递一条记录。正如我所提到的,API 是由其他人创建的。
猜你喜欢
  • 1970-01-01
  • 2016-06-16
  • 2020-12-24
  • 1970-01-01
  • 1970-01-01
  • 2015-01-20
  • 2020-07-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多