Reputation: 55
I want to have a vb class with properties as follows:
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.
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
Upvotes: 1
Views: 6195
Reputation: 22456
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)
:
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:
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.
Upvotes: 1
Reputation: 4753
You can do in this way:
ListBox.Items.AddRange(dicvtionary.Keys.ToArray())
Hope this helps..
Upvotes: 2