vb.net 类属性中的可视化基本集合(列表)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20867642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:18:36  来源:igfitidea点击:

visual basic collection (list) in class properties

vb.net

提问by Kevin Sullivan

I want to have a vb class with properties as follows:

我想要一个具有如下属性的 vb 类:

Public Class aClass

Private dict As Dictionary(Of String, Integer)

Public Sub New()
    dict = New Dictionary(Of String, Integer)
    dict.Add("something", 2)
    dict.Add("something other", 4)
End Sub

Public ReadOnly Property Prop As List(Of String)

    Get
        Dim newList As New List(Of String)
        For Each d As String In dict.Keys
            newList.Add(d)
        Next
        Return newList
    End Get

End Property

The property method will iterate over all the dictionary entries and add the keys to a list.

property 方法将遍历所有字典条目并将键添加到列表中。

How do I then call this property method within a form and display the list within a list box?

然后如何在窗体中调用此属性方法并在列表框中显示列表?

    Public Class Form1
Dim f As aClass = New aClass
Public Sub New()

    ' This call is required by the designer.
    InitializeComponent()
    upDateView()

    ' Add any initialization after the InitializeComponent() call.

End Sub

Public Sub upDateView()
    ListBox1.Items.Clear()
    ListBox1.Items.Add(f.Prop)'This is where I get really stuck??

End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)    Handles Button1.Click

    f.addToDict(TextBox1.Text)'This method has been created
    upDateView()

End Sub
End Class

I have scoured google for over a day and half but finally I have conceded to ask for some help, I am a newbie so please be patient if this is easy for you guys.

我已经在谷歌上搜索了超过一天半的时间,但最后我承认寻求一些帮助,我是一个新手,所以如果这对你们来说很容易,请耐心等待。

Thanks

谢谢

kevin

凯文

回答by Sai Avinash

You can do in this way:

你可以这样做:

ListBox.Items.AddRange(dicvtionary.Keys.ToArray())

Hope this helps..

希望这可以帮助..

回答by Markus

I'd propose not exposing a list as a property because the caller can alter the list. In your sample, these changes will not be reflected in the data, so the capability is useless for the caller. If the caller wants a list, he or she can create one. If you just want to publish an enumeration of the keys, the safest way is to create a property of type IReadOnlyCollection(Of T):

我建议不要将列表作为属性公开,因为调用者可以更改列表。在您的示例中,这些更改不会反映在数据中,因此该功能对调用者来说毫无用处。如果呼叫者想要一个列表,他或她可以创建一个。如果您只想发布键的枚举,最安全的方法是创建一个 type 属性IReadOnlyCollection(Of T)

Public Class aClass

    Private dict As Dictionary(Of String, Integer)

    Public Sub New()
        dict = New Dictionary(Of String, Integer)
        dict.Add("something", 2)
        dict.Add("something other", 4)
    End Sub

    Public ReadOnly Property Prop As IReadOnlyCollection(Of String)
        Get
            Return dict.Keys.ToList().AsReadOnly()
        End Get    
    End Property
End Class 

This way, the caller cannot tamper with the list items (it is good that in your case the caller would not have been able to change the underlying collection of dictionary keys as you create a separate list). In order to create a list based upon the Keys, above sample uses the ToList() extension method. The AsReadOnly() method then returns a read only list that cannot be changed.
In order to add the items to the ListBox, use the following code:

这样,调用者就无法篡改列表项(在您的情况下,调用者在您创建单独的列表时无法更改字典键的基础集合是很好的)。为了创建基于键的列表,上面的示例使用 ToList() 扩展方法。AsReadOnly() 方法然后返回一个无法更改的只读列表。
要将项目添加到 ListBox,请使用以下代码:

ListBox1.Items.AddRange(cls.Prop.ToArray())

Please note that the code involves a lot of conversions of collections (Keys -> ToList -> AsReadOnly -> ToArray). In this case, it would also be an alternative to change the type of the property to an array of strings (String()) and use Keys.ToArray() as the only conversion that is necessary. If the callers of the class are in the same assembly, this is a good way to simplify things.

请注意,代码涉及到很多集合的转换(Keys -> ToList -> AsReadOnly -> ToArray)。在这种情况下,将属性的类型更改为字符串数组 ( String()) 并使用 Keys.ToArray() 作为唯一必要的转换也是一种替代方法。如果类的调用者在同一个程序集中,这是简化事情的好方法。