How to stop my reference onject from being changed RRS feed

  • Question

  • I have a class. In that class is a readonly property the returns a List(Of T) in the class. I dont want that list to be changed. I understand that readonly only actually applies to the pointer and not the object being pointed to. That's fine. But how do I stop people from changing the list?
    Wednesday, November 9, 2011 7:48 PM

All replies

  • You don't.

    You could make your own Collection class that is immutable, but if you give some other section of code a reference to a list, you can't stop them from changing it.

    You could make a deep copy of the list, and only ever give out that copy so that the original list is never edited elsewhere as an alternative.

    Wednesday, November 9, 2011 7:51 PM
  • You could use a ReadOnlyCollection(of T)

        Private MyBaseList As List(Of String)

        Public ReadOnly Property MyList As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
                Return New System.Collections.ObjectModel.ReadOnlyCollection(Of String)(MyBaseList)
            End Get
        End Property

    Stephen J Whiteley
    Wednesday, November 9, 2011 7:55 PM
  • Let me rephrase this. I have a custom collection class. Inside that class is a list(of t). I want the interior list to be able to be changed but only with the methods in the collection class. I have another class that contains an instance of this custom collection class. That class has the property that gives the custom collection.


    Public Class RoutePointCollection : Implements IEnumerable

           Private ReadOnly _routePoints As List(Of RoutePoint)


    End Class


    Public Class Route

        Private _path As RoutePointCollection

        Public ReadOnly Property Path() As RoutePointCollection
                Return _path
            End Get
        End Property


    End Class

    Wednesday, November 9, 2011 8:09 PM
  • Your requirement is not clear.  Do you mean that you want to expose a collection property that does not support changes to the items in the collection?  That is, the user gets an object that has a list of items that they can reference by an index, but they cannot add to, remove, or change any of the items?    If so, you need to define you own collection type.  Pass the user a clone of the original object if you don't want subsequent changes to that object (made through the property sets in the class that instantiated it) to appear in the user's copy, or pass the user a reference to the object if you do want changes to that object to appear in the user's copy. 

    Or do you mean that the user will get a collection object which they can make changes to, but those changes will never affect the original collection object from which the user's object was created (and changes in the original collection will not appear in the user's copy)?  In that case, use any of the standard collection types, but pass the user a clone of the original collection instance.

    Wednesday, November 9, 2011 9:42 PM