what type of collection to use?  
Author Message
Anarchy





PostPosted: Visual Basic Language, what type of collection to use? Top

I have a collection of type System.Collections.Specialized.NameValueCollection, which is great for getting the value of each item using either the Key or Index.

However, I also need to be able to get the Name of a Key using it's index, and this doesn't seem possible with this type of collection. The only way I can see to do it is by keeping two collections going, the second with the name of the key in the first collection, that can't be right!

Anyone out there who knows about collections can tell me a better way

Thanks


Visual Basic9  
 
 
cgraus





PostPosted: Visual Basic Language, what type of collection to use? Top

There is no built in collection that maintains a name/value pair in both directions. The obvious solution is to store the values in both directions, if they are both of the same type.



 
 
Anarchy





PostPosted: Visual Basic Language, what type of collection to use? Top

thanks cgraus, that's about what I'm doing

 
 
DMan1





PostPosted: Visual Basic Language, what type of collection to use? Top

I have a collection of type System.Collections.Specialized.NameValueCollection, which is great for getting the value of each item using either the Key or Index.

However, I also need to be able to get the Name of a Key using it's index, and this doesn't seem possible with this type of collection. The only way I can see to do it is by keeping two collections going, the second with the name of the key in the first collection, that can't be right!

Anyone out there who knows about collections can tell me a better way

Thanks

Create you own collection that inherits from NameValueCollection and then add your own properties that gets the name of the key by index..This way you will only work with a single collection!



 
 
Anarchy





PostPosted: Visual Basic Language, what type of collection to use? Top

"gets the name of the key by index" exactly! But how do you do that

I've said it before, etc... The new collections in VB are carp, and so is the documentation on how to use the ****s.

I've been re-writing this all day, I have a class which inherits System.Collections.Specialized.NameObjectCollectionBase, I add custom object (with 2 strings), then when I want to do a for each blah in blah, the object I get back is a string, not my custom object. Now I think I need to implement iEnumerable and some other bollox, WHY OH WHY OH WHY do I have to write code for something that vb.net should be doing for me.

I tell ya, vb.net has taken some backward steps from vb 6, and that's saying some.

Control arrays, collections, streams, all worse (or at least too ****y compicated) than they were

*rant mode off*

 
 
DMan1





PostPosted: Visual Basic Language, what type of collection to use? Top

While setting up an example of the inherited class I noticed that the namevalue collection has a getkey(byval Index as integer) method which will return the name of the key by index...is this not what you want

But here is an example of inheriting from generic collection and implementing a get by index:

Public Class CountyDataSetCollection

Inherits System.Collections.CollectionBase

' Restricts to CountyDataSet types, items that can be added to the collection.

Public Sub Add(ByVal DS As CountyDataSet)

' Invokes Add method of the List object to add a CountyDataSet.

List.Add(DS)

End Sub

Public Sub Remove(ByVal index As Integer)

' Check to see if there is a SPD at the supplied index.

If index > Count - 1 Or index < 0 Then

' If no SPD exists, a messagebox is shown and the operation is

' cancelled.

System.Windows.Forms.MessageBox.Show("Index not valid!")

Else

' Invokes the RemoveAt method of the List object.

List.RemoveAt(index)

End If

End Sub

Public Sub Remove(ByVal Name As String)

Dim x As Integer = 0

For Each DS As CountyDataSet In Me

If DS.Name = Name Then

List.RemoveAt(x)

Exit Sub

End If

x += 1

Next

System.Windows.Forms.MessageBox.Show("Name not valid!")

End Sub

' declares that it will return a SPD object.

Public ReadOnly Property Item(ByVal index As Integer) As CountyDataSet

Get

Return CType(List.Item(index), CountyDataSet)

End Get

End Property

Public ReadOnly Property Item(ByVal Name As String) As CountyDataSet

Get

Dim x As Integer

For Each DS As CountyDataSet In Me

If DS.Name = Name Then

Return CType(List.Item(x), CountyDataSet)

Exit Property

End If

x += 1

Next

Return Nothing

End Get

End Property

End Class



 
 
Anarchy





PostPosted: Visual Basic Language, what type of collection to use? Top

looks good, I'll try it later.

Collections are still to hard tho