.NET默认属性错误 [英] .NET Default Properties Error

查看:104
本文介绍了.NET默认属性错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个VB.NET项目,在其中可以使用索引遍历字典对象的键和值集合:

I have a VB.NET project where I am able to iterate through the keys and values collections of a dictionary object using an index:


MyDictionary.Keys(idx)
MyDictionary.Values(idx)

将此代码从测试项目中提取并放入真实项目时,会出现以下错误:

When this code is taken from the test project and placed into the real project I get the following error:



'System.Collections.Generic.Dictionary(Of Double,String).KeyCollection'无法建立索引,因为它没有默认属性。

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




'System.Collections.Generic.Dictionary(由于没有默认属性,因此无法对ValueCollection'进行索引。

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


此正在使用VB.NET和VS2008。我不知道从一个项目到下一个项目的不同会导致此错误。该测试是一个控制台应用程序,而该程序是一个winforms应用程序。

This is using VB.NET and VS 2008. I don't know what the difference would be from one project to the next that would cause this error. The test is a console application and the program is a winforms app.

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

What conditions would cause the default property of these collections to change?

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

Edit - Thank you for all of the answers that tell me how to loop through a dictionary. Those, answers, however, do not answer my question of why I can use an index in one project and not the other. Should I not be able to copy and paste the code from one .net project to another and have it work the same? And, no, option strict, is not the cause of the problem.

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

Edit - Attempt to reproduce what I'm seeing:


  • 使用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为Integer)作为Double- index:要检索的索引的从零开始的元素。

In the line in sub search that says "dtf.Keys(idx) = 0" place your cursor after the right parenthesis and backspace you should get a tooltip that says, "<Extension> ElementAtOrDefault(index as Integer) as Double - index: the zero based element of the index to retrieve.

我没有得到其他项目。即使看起来我具有相同的引用和设置。

I am not getting that in my other project. Even though it seem I have the same references and settings.

推荐答案

KeyCollection并没有实现这样的索引器,您必须通过MyDictionary.Keys。

KeyCollection does not implement indexers like that, you must enumerate through the 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 ++;)循环不是遍历哈希表(字典)的正确方法,因为它不是一个数组,它真的没有数组索引(array [index])的概念

Looping with a for(;i++;) wouldn't be the correct way of going through your hashtable (dictionary) since it is not an array it really has no concept of an array index (array[index])

这篇关于.NET默认属性错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆