Reading values from NameObjectCollectionBase

  • Thread starter Thread starter \(O\)enone
  • Start date Start date
O

\(O\)enone

I've written some code that reads the names and values from a
Collections.Specialized.NameValueCollection and displays them on the screen.

However, I'd ideally like to change this code so that it runs against the
abstract Collections.Specialized.NameObjectCollectionBase class, so that all
collections that derive from this base class (which includes
NameValueCollection) will work with my code.

I can extract the keys that have been used to add items to the collections
using the Keys() property which is implemented in the base class, but there
is no similar method to extract the values that are associated with the
keys. As the class appears by definition to hold matching pairs of names
(keys) and values, I would have thought there would be a way to obtain the
values themselves.

Can anyone shed any light on this?

Thanks in advance,
 
(O)enone said:
I've written some code that reads the names and values from a
Collections.Specialized.NameValueCollection and displays them on the
screen.

However, I'd ideally like to change this code so that it runs against the
abstract Collections.Specialized.NameObjectCollectionBase class, so that
all collections that derive from this base class (which includes
NameValueCollection) will work with my code.

I can extract the keys that have been used to add items to the collections
using the Keys() property which is implemented in the base class, but
there is no similar method to extract the values that are associated with
the keys. As the class appears by definition to hold matching pairs of
names (keys) and values, I would have thought there would be a way to
obtain the values themselves.

Can anyone shed any light on this?

Thanks in advance,

Not very well named but the BaseGet function will take the key or an integer
as a parameter and return the value as an object.

Hope this helps.

Lloyd Sheen
 
Lloyd said:
Not very well named but the BaseGet function will take the key or an
integer as a parameter and return the value as an object.

Thanks for your assistance, Lloyd.

I settled on BaseGetAllKeys and BaseGetAllValues in the end to get the keys
and values for the whole collection in one go.

However, these are both protected members, and so the only way I can get
into them is to use reflection to identify the members and then Invoke them.
It works (at least with all the inherited object types I've tested with),
but it's pretty nasty and hacky. Is there a cleaner way that I could access
those members than this?

Thanks,
 
Back
Top