从C#转换的vb.net代码中获取“类型错误”

时间:2013-01-04 21:59:43

标签: vb.net visual-studio-2010

我在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

2 个答案:

答案 0 :(得分:0)

新()什么?你在那里缺少一个对象名称。你现在正在传递一个匿名对象。

答案 1 :(得分: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