views:

57

answers:

1

Hi*,

Dim BigCollection As New Collection
Dim SmallCollection As New Collection
SmallCollection.Add("Hello World")
BigCollection.Add(SmallCollection)

MsgBox(BigCollection(1)(1)) 'shows "Hello World
SmallCollection.Clear()
MsgBox(BigCollection(1)(1)) 'ERROR (Collection is empty)

I want that once I put something in the BigCollection it stays there, I dont want it to be changeable from outside. I want to be able to clear the SmallCollection but it should not be cleared in the BigCollection. Any Ideas? Maybe it is bad approach, I am not the .net guru here yet ;)

Thanks!

+2  A: 

If you want a collection that shouldn't ever change, then you should expose it via a ReadOnlyCollection(Of T). Creating one from a standard Collection type has fairly awkward syntax though.

Dim completeSmall As New ReadOnlyCollection(Of Object)(SmallCollection.Cast(Of Object))

It would be easier if you started out with List(Of Object) instead of Collection. The final syntax is a bit easier to read

Dim SmallCollection As New List(Of Object)()
...
Dim completeSmall As New ReadOnlyCollection(Of Object)(SmallCollection)
JaredPar