【问题标题】:Unable to cast object of type 'System.Collections.Generic.List`1[System.Int32]' to type 'crmWebService.ArrayOfInt'无法将“System.Collections.Generic.List`1[System.Int32]”类型的对象转换为“crmWebService.ArrayOfInt”类型
【发布时间】:2012-05-22 02:34:49
【问题描述】:

将列表发送到网络服务后出现错误。

这是调用网络服务的代码:

Dim sProgramInterest As New List(Of Integer)    
crmService.InsertProspectGetId(sProgramInterest.ToList)

但是我收到了这个错误。

无法将“System.Collections.Generic.List`1[System.Int32]”类型的对象转换为“crmWebService.ArrayOfInt”类型。

web服务接受的参数是:

ByVal sProgramInterest As List(Of Integer)

【问题讨论】:

  • 看起来 crmWebService.ArrayOfInt 是自定义类型。你能发布它的定义吗?右键单击 InsertProspectGetId 并转到定义。它的参数是 ArrayOfInt 类型的 去它的定义,你能发布它吗?

标签: .net vb.net exception-handling casting


【解决方案1】:

我假设你必须做类似的事情,

Dim sProgramInterest As New crmWebService.ArrayOfInt    
crmService.InsertProspectGetId(sProgramInterest)

根据错误,参数必须是crmWebService.ArrayOfInt 类型。您必须使用该类而不是 List(Of Integer)。

【讨论】:

    【解决方案2】:

    似乎服务接受 int 数组,而不是 int 列表。因此尝试:

    crmService.InsertProspectGetId(sProgramInterest.ToArray())
    

    【讨论】:

    • 我认为你的答案是在 C# 中。应该是 ToArray 对于 vb.net
    • @Youssef - 你错了。 ToArray 和 ToArray() 都在 VB.NET 中工作。当方法调用是无参数的时,() 在 VB.NET 中是可选的。
    • @Youssef:上次我使用 VB.NET 时,我接受了两种样式 - 带括号和不带括号,所以它是可选的。 IMO 丑陋并抹去了 VB.NET 程序员的大脑。
    • 很高兴知道。谢谢。我从未使用过 VB.NET,但我知道 {} 和 () 比 C# 少得多。不知道两者都支持
    • 当我这样做时 --- 我得到这个错误 Error 1 Value of type '1-dimensional array of Integer' cannot be convert to 'crmWebService.ArrayOfInt'
    【解决方案3】:

    不要调用ToList,而是调用ToArray:

    crmService.InsertProspectGetId(sProgramInterest.ToArray)
    

    【讨论】:

    • 当我这样做时 --- 我收到此错误错误 1 ​​类型“整数的一维数组”的值无法转换为“crmWebService.ArrayOfInt”
    • @JeremyLewallen - 好的。我认为ArrayOfInt 是一个整数数组的假设似乎是不正确的。 crmWebService.ArrayOfInt 是什么?它是如何定义的?
    猜你喜欢
    • 2013-10-06
    • 1970-01-01
    • 2015-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-14
    相关资源
    最近更新 更多