加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

vb.net – .NET默认属性错误

发布时间:2020-12-17 00:33:09 所属栏目:大数据 来源:网络整理
导读:参见英文答案 Why can I access an item in KeyCollection/ValueCollection by index even if it doesn’t implement IList(Of Key)?2个 我有一个VB.NET项目,我可以使用索引迭代键和值字典对象的集合: MyDictionary.Keys(idx)MyDictionary.Values(idx) 当此
参见英文答案 > Why can I access an item in KeyCollection/ValueCollection by index even if it doesn’t implement IList(Of Key)?2个
我有一个VB.NET项目,我可以使用索引迭代键和值字典对象的集合:
MyDictionary.Keys(idx)
MyDictionary.Values(idx)

当此代码从测试项目中获取并放入实际项目时,我收到以下错误:

‘System.Collections.Generic.Dictionary(Of Double,String).KeyCollection’ cannot be indexed because it has no default property.

‘System.Collections.Generic.Dictionary(Of Double,String).ValueCollection’ cannot be indexed because it has no default property.

这是使用VB.NET和VS 2008.我不知道从一个项目到下一个会导致此错误的项目有什么区别.测试是一个控制台应用程序,该程序是一个winforms应用程序.

什么条件会导致这些集合的默认属性发生变化?

编辑 – 感谢所有答案,告诉我如何循环字典.然而,这些答案并没有回答我为什么可以在一个项目中使用索引而不在另一个项目中使用索引的问题.我是否应该无法将代码从一个.net项目复制并粘贴到另一个.net项目并使其工作相同?并且,不,选项严格,不是问题的原因.

编辑 – 尝试重现我所看到的内容:

>使用VS 2008创建新的VB.NET控制台应用程序
>将以下代码复制并粘贴到模块中:

Imports System.Collections
Imports System.Collections.Generic

Module Module1

    Public dtf As Dictionary(Of Double,String)

    Public Sub BuildDictionary()

        dtf = New Dictionary(Of Double,String)

        dtf.Add(1.0,"1")
        dtf.Add(0.0,"0")

    End Sub

    Public Sub Search()
        For idx As Integer = 0 To dtf.Keys.Count - 1
            If dtf.Keys(idx) = 0 Then
                Exit Sub
            End If
        Next
    End Sub

    Sub Main()

    End Sub

End Module

在子搜索中显示“dtf.Keys(idx)= 0”的行中,将光标放在右括号和退格后,您应该得到一个工具提示,其中“< Extension> ElementAtOrDefault(index as Integer)为Double – index :要检索的索引的基于零的元素.

我没有在我的其他项目中得到它.即使看起来我有相同的参考和设置.

KeyCollection没有实现这样的索引器,你必须通过MyDictionary.Keys枚举.

C#

foreach(double key in MyDictionary.Keys)
 Console.Write( MyDictionary[ key ] )

VB

For Each key As Double in MyDictionary.Keys
   Console.Write( MyDictionary( key )
Next key

用for(; i;)循环不是正确的方法来通过你的哈希表(字典),因为它不是一个数组,它真的没有数组索引的概念(数组[索引])

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读