Altering collection elements in place

  • Thread starter Thread starter Jeff Stewart
  • Start date Start date
J

Jeff Stewart

Let's say I create a collection of DateTime objects like so:

Dim clxn_Times As Collection = New Collection()

Let's go on to say I add 3 elements to the collection. How do I pull off
the result represented by the following?

clxn_Times(0) = clxn_Times(0).AddDays(-1)

I can't find a way to edit an item in place. But not knowing very much
about Visual Basic (or how to phrase this problem so I can find it in the
documentation), I can't figure out how to get a "reference" to an element so
that the element can be altered in place.
 
Hi Jeff,

For me a lot of stuff from the MicrosoftVisual.basic namespace is handy,
some I do not use and one I do not like. That is the the
Microsoft.Visual.Basic collection.

It works so different from the rest of the dotNet stuff, I advice you to
have a look at the arraylist, a very handy class to make collections.

And when that does not fit you can have a look at hashtable, or sortedlist

I hope this helps?

Cor
 
* "Jeff Stewart said:
Let's say I create a collection of DateTime objects like so:

Dim clxn_Times As Collection = New Collection()

Let's go on to say I add 3 elements to the collection. How do I pull off
the result represented by the following?

clxn_Times(0) = clxn_Times(0).AddDays(-1)

I can't find a way to edit an item in place. But not knowing very much
about Visual Basic (or how to phrase this problem so I can find it in the
documentation), I can't figure out how to get a "reference" to an element so
that the element can be altered in place.

You will have to remove and re-add the item (seem methods 'Remove' and
'Add' of the collection).
 
Jeff,
I like the collections better than things like arrays. And as far as I know,
MS uses them for all their storage, like controlscollections and so on.
Edit in place? You store the reference to an object in a collection. So if
you 'take out' an object from the collection it's just a pointer. After
editing u don't have to put it back into the collection to save it.

Frank
 
* "Frank said:
I like the collections better than things like arrays. And as far as I know,
MS uses them for all their storage, like controlscollections and so on.
Edit in place? You store the reference to an object in a collection. So if
you 'take out' an object from the collection it's just a pointer. After
editing u don't have to put it back into the collection to save it.

.... that's true for 'ArrayList' and most other collections in the
'System.Collections' namespace.
 
Jeff,
It appears that you have a collection of DateTime (Date) values. Correct?

System.DateTime is a value type, when you add a DateTime to a collection the
value will be boxed (copied to the heap) when you get the value from the
collection a copy of the boxed value will be made, when you use AddDays a
copy of the value will be made. In other words for DateTime values your code
is the way to do it, as your code is the way it works!

Reference Types (such as Control) exist on the Heap, when you add a Control
to a collection the reference will be copied to the collection, only a
single Control object will exist on the heap...

As Cor suggested. I would also recommend you look at & use the Collection
classes in System.Collections & System.Collections.Specialized instead of
the Micrososft.VisualBasic.Collection class. As Herfried suggests the
ArrayList (from System.Collections) will allow your code with casting,
because the ArrayList holds objects, your DateTime values will need to be
boxed going in (Add, Item set), and unboxed coming out (Item get, For Each)
of the ArrayList.

NOTE: In VS.NET 2005 (Whidbey) due out sometime in 2005 will support
Generics, you will be able to very easily make a strongly typed "ArrayList"
class, no boxing involved.

Hope this helps
Jay
 
Back
Top