0

我在 VB.Net 2010 中有这段代码,我在 New() 上收到“类型错误”。

此代码是从 C# 转换而来的。

我究竟做错了什么?

Public Function CredentialGet(ByVal sKey As String, ByRef sCred As String)
    Dim sCredential As Element.Credential

    sCredential  = apiclient.SearchCredentials(sSoftwareKey, SessionID,
        New() {New Element.SearchTerm() With {.FilterKey = "APK", .Value = sKey}})
    sCred = sCredential.CredentialID
End Function
4

2 回答 2

0

New() what? You are missing an object name there. You're now passing in an anonymous object.

于 2013-06-04T20:32:39.710 回答
0

删除匿名类型的括号。VB.Net 在这种情况下不使用它们,而是寻找With关键字。并且函数应该返回一个值。您不返回任何内容,因此请使用 Sub:

Public Sub CredentialGet(ByVal Key As String, ByRef Cred As String)
    Dim Credential As Element.Credential 
    Credential = apiclient.SearchCredentials(sSoftwareKey, SessionID, _
        New With {New Element.SearchTerm() With {.FilterKey = "APK", .Value = Key}})
    Cred = Credential.CredentialID
End Sub 

我也质疑这个设计。最好返回一个字符串:

Public Function CredentialGet(ByVal Key As String) As String
    Return apiclient.SearchCredentials(sSoftwareKey, SessionID, _
        New With {New Element.SearchTerm() With {.FilterKey = "APK", .Value = Key}}).CredentialID
End Function
于 2013-06-04T21:25:23.107 回答