Are collections not meant for modifying?

  • Thread starter Thread starter Slonocode
  • Start date Start date
S

Slonocode

I have a hashtable full of objects. There seems to be no way to get an
object from the hashtable and modify it. Are collections or hashtables in
particular not meant for this kind of operation?
 
* "Slonocode said:
I have a hashtable full of objects. There seems to be no way to get an
object from the hashtable and modify it. Are collections or hashtables in
particular not meant for this kind of operation?

You can simply modify it by changing the properties of the object stored
in the collection. Notice that you will have to cast to the type of the
object in order to access the properties:

\\\
DirectCast(foo, Goo).Baz = "..."
///
 
Herfried K. Wagner said:
You can simply modify it by changing the properties of the object stored
in the collection. Notice that you will have to cast to the type of the
object in order to access the properties:

\\\
DirectCast(foo, Goo).Baz = "..."
///


Thank you that works. Would you be able to explain more how this is working
I'm not sure I understand it exactly? What am I casting from?
 
Slonocode,
There seems to be no way to get an
object from the hashtable and modify it.
Use HashTable.Item to return a reference to one of the objects in the
collection, use mutable methods on this object to modify the object itself.

As Herfried suggested you may need to cast the object returned by
HashTable.Item to the type it actually is.
Are collections or hashtables in
particular not meant for this kind of operation?
NO! collections & hashtables are entirely meant for this kind of operation!

Only time this doesn't really work is if you store value types (Integers,
Structures) in an ArrayList or HashTable, as the value type will be boxed,
and the boxed value will actually be stored.


Hope this helps
Jay
 
* "Slonocode said:
Thank you that works. Would you be able to explain more how this is working
I'm not sure I understand it exactly? What am I casting from?

You are casting from 'Object', because the collection will hold objects
of any type that derives from 'Object'.
 
Back
Top