【问题标题】:Getting a "Type Error" in vb.net code converted from C#在从 C# 转换的 vb.net 代码中获取“类型错误”
【发布时间】:2013-01-04 21:59:43
【问题描述】:

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

【问题讨论】:

    标签: vb.net visual-studio-2010


    【解决方案1】:

    New() 什么?您在那里缺少一个对象名称。你现在传入了一个匿名对象。

    【讨论】:

      【解决方案2】:

      删除匿名类型的括号。 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
      

      【讨论】:

        猜你喜欢
        • 2022-01-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-17
        • 1970-01-01
        • 2011-09-25
        • 2013-08-24
        • 1970-01-01
        相关资源
        最近更新 更多