【问题标题】:Overload resolution failed because no accessible 'Item' can be called without a narrowing conversion重载解决失败,因为没有缩小转换就无法调用可访问的“项目”
【发布时间】:2015-05-17 17:01:19
【问题描述】:

所以,我正在尝试按照 this 指南从 Visual Basic 中的 XML 数据填充树视图,但它会引发错误:

“重载解析失败,因为没有可访问的‘项目’ 在没有缩小转换的情况下调用: 'Public Overridable ReadOnly Default Property Item(key As String) As System.Windows.Forms.TreeNode':参数匹配参数'key' 从“长”缩小到“字符串”。 'Public Overridable Default Property Item(index As Integer) As System.Windows.Forms.TreeNode':参数匹配参数'index' 从 'Long' 缩小到 'Integer'。”

经过一番搜索,我被告知将 Option Strict 设置为“关闭”可以解决错误,但它没有做这样的事情。有没有人解决这个错误?

这里是有问题的代码:

 If inXmlNode.HasChildNodes() Then
     nodeList = inXmlNode.ChildNodes
     For i = 0 To nodeList.Count - 1
        xNode = inXmlNode.ChildNodes(i)
        inTreeNode.Nodes.Add(New TreeNode(xNode.Name))
        tNode = inTreeNode.Nodes(i)
        AddNode(xNode, tNode) //The code that throws the error.
     Next
  Else
     ' Here you need to pull the data from the XmlNode based on the
     ' type of node, whether attribute values are required, and so forth.
     inTreeNode.Text = (inXmlNode.OuterXml).Trim
  End If

【问题讨论】:

  • 如果您发布代码的相关部分并指出哪个语句出现错误会有所帮助。您似乎正在使用Long 来获取特定项目,而Item 方法需要IntegerString。很可能您应该只使用Integer 而不是Long
  • 我的代码与我上面链接的代码完全相同,但它出错的代码尤其是这段代码。 'AddNode(xNode, tNode)'
  • 您引用的语句调用 AddNode 方法,该方法需要两个 TreeNode 对象作为其参数(您已提供)。当需要IntegerString 时,很难理解编译器如何认为您提供了Long。你确定不是前面的语句得到错误吗?
  • 我很确定,here's 也是一张 Visual Studio 的照片。
  • 再看图,被标记的语句确实是前一个tNode = inTreeNode.Nodes(i)。由于给出的原因无法编译;它期望iIntegerString。您可以通过将 i 声明为 Integer 来解决此问题,尽管我不知道将其声明为“Long”还有其他原因。

标签: vb.net overloading resolution strict


【解决方案1】:

将 i 定义为 Integer 而不是 Long。那么它应该匹配过载。

 Private Sub AddNode(ByRef inXmlNode As XmlNode, ByRef inTreeNode As TreeNode)
      Dim xNode As XmlNode
      Dim tNode As TreeNode
      Dim nodeList As XmlNodeList
      Dim i As Integer


    If inXmlNode.HasChildNodes() Then
         nodeList = inXmlNode.ChildNodes
         For i = 0 To nodeList.Count - 1
            xNode = inXmlNode.ChildNodes(i)
            inTreeNode.Nodes.Add(New TreeNode(xNode.Name))
            tNode = inTreeNode.Nodes(i)
            AddNode(xNode, tNode) //The code that throws the error.
         Next
      Else
         ' Here you need to pull the data from the XmlNode based on the
         ' type of node, whether attribute values are required, and so forth.
         inTreeNode.Text = (inXmlNode.OuterXml).Trim
      End If

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多