 |
|
 |
NameValueCollection is supposed to allow multiple values per key.
|
|
|
|
 |
|
 |
Not the same functionality, but a that is also able to access items by index or by key is the KeyedCollection generic class.
I use an inherited version of it, which discovers the Key for an item automatically. But, as I said, it is not the SAME functionality, as the keys are got from their values. Initially it could not look so great, but I use them when working with records (as the "primary key" is it's key) or even when working with "named objects" that contain their own names (components, fields and so on).
|
|
|
|
 |
|
 |
A NameValueCollection is a key with multiple values.
This is no Generic NameValueCollection, it's just another Dictionary.
Small but VERY important quote from the MSDN about the NameValueCollction:
"This collection is based on the NameObjectCollectionBase class. However, unlike the NameObjectCollectionBase, this class stores multiple string values under a single key."
Yllusio.
I'm not responsible for what I didn't say and hardly for what I did say.
|
|
|
|
 |
|
 |
Bravo. Worked out of the box first time - saved me a lot of time. I was setting out to write something similar, but in checking for any existing solutions I found yours. Thanks for sharing!
|
|
|
|
 |
|
 |
And it's a shame .NET doesn't have it's own NameObjectCollection<>-class.
For all those critics: no, there is no generic, Dictionary<,>-like class which can access elements by key AND by position. I searched all the namespaces, and this kind of class is simply missing. The only class providing this function is NameObjectCollectionBase, which you need to derive from in order to use it - which the author did for us in a generic way, so it can be reused.
|
|
|
|
 |
|
 |
You might want to look in the System.Collections.ObjectModel Namespace. It contains Generic classes for Indexed + Keyed Collections
|
|
|
|
 |
|
 |
Hmm i've checked also this, but haven't found anything.
If you know which class supports:
a) generic key / value collections
b) getters by: KEY and POSition
then let me know...
Me was also assuming, that .net must have such a class - but the only such class i found was the NameObjectCollection - from which our class inherits... Still myself I think that this clas of mine is a small stuff, but i'm amazing that i havent found an existing equivalent in .net...
C#, ASPX, SQL, novice to NHibernate
|
|
|
|
 |
|
 |
As I said they are in the namespace System.Collections.ObjectModel
Below is is an example in VB.Net
Regards
Neal
'The Value Object Class
Public Class SomeObject
Public Sub New(ByVal key As String)
Me.Key = key
End Sub
Public Key As String
End Class
'The Collection
Public Class NameValueCollection
Inherits System.Collections.ObjectModel.KeyedCollection(Of String, SomeObject)
Protected Overrides Function GetKeyForItem(ByVal item As SomeObject) As String
Return item.Key
End Function
End Class
Private Shared Sub Main()
Dim a As New NameValueCollection
a.Add(New SomeObject("AKey"))
Dim MyObject1 As SomeObject = a.Item(0) 'By Index
Dim MyObject2 As SomeObject = a.Item("AKey") 'By Key
End Sub
|
|
|
|
 |
|
 |
Nice work, but...
Check it out in detail:
- NameValueCollection: key=string, value=string
- KeyedCollection is an abstract type: you must inerit it - so almost the same as NameObjectCollection.
So both KeyedCollection and NameObjectCollection are a good start. The new class of mine is derived from the
abstract NameObjectCollection (but it could also be derived from KeyedCollection - although KeyedCollection because any type of strong key is allowed, would be better).
So the only meaning for my class is to avoid deriving classes (for each strong type) i want to use in collections. My class can be initialized via a simple constructor...
Hope this time with this explanation i've got the whole point of this small class...
C#, ASPX, SQL, novice to NHibernate
|
|
|
|
 |
|
 |
Another useless article on The Code Project. Hooray!
|
|
|
|
 |
|
 |
Actually the only USELESS stuff around this article is YOUR COMMENT:
There's no usefull suggestion, neither a hint why this class is useless. My code is at least in use, how about your comment?
(i hope you liked my message as much as i did yours)
So ANYHOW:
I know this code is not too much, but spent cca. 30 mins for research, coding and commenting only to get a NameValueCollection class which supports any type of object easily. Maybe there's a similar class, i've missed in the MSDN...
C#, ASPX, SQL, novice to NHibernate
|
|
|
|
 |
|
 |
What's wrong with using a generic IDictionary with string keys and "what ever type you'd like to use" as values? Also, the AllValues property should return an array of TValue, not of object.
|
|
|
|
 |
|
 |
Generic IDictionary does not allow you to get object by position
So the introduction of the article is modified. Thanx for the comment. Btw: i know this article is not too much, but can spare some time. Article's mentioned for lazy programmers.
Have a nice day.
C#, ASPX, SQL, novice to NHibernate
|
|
|
|
 |
|
 |
Yeah. I agreed.
I'm not sure why we need "Generic NameValueCollection".
|
|
|
|
 |