Export (0) Print
Expand All

CollectionBase.OnInsert Method

Performs additional custom processes before inserting a new element into the CollectionBase instance.

Namespace:  System.Collections
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
Protected Overridable Sub OnInsert ( _
	index As Integer, _
	value As Object _
)

Parameters

index
Type: System.Int32

The zero-based index at which to insert value.

value
Type: System.Object

The new value of the element at index.

The default implementation of this method is intended to be overridden by a derived class to perform some action before the specified element is inserted.

The On* methods are invoked only on the instance returned by the List property, but not on the instance returned by the InnerList property.

If the process fails, the collection reverts back to its previous state.

The default implementation of this method is an O(1) operation.

Notes to Implementers

This method allows implementers to define processes that must be performed before inserting the element into the underlying System.Collections.ArrayList. By defining this method, implementers can add functionality to inherited methods without having to override all other methods.

OnInsert is invoked before the standard Insert behavior, whereas OnInsertComplete is invoked after the standard Insert behavior.

For example, implementers can restrict which types of objects can be inserted into the System.Collections.ArrayList.

OnValidate is called prior to this method.

The following code example implements the CollectionBase class and uses that implementation to create a collection of Int16 objects.

Imports System
Imports System.Collections


Public Class Int16Collection
   Inherits CollectionBase


   Default Public Property Item(index As Integer) As Int16
      Get 
         Return CType(List(index), Int16)
      End Get 
      Set
         List(index) = value
      End Set 
   End Property 


   Public Function Add(value As Int16) As Integer 
      Return List.Add(value)
   End Function 'Add

   Public Function IndexOf(value As Int16) As Integer 
      Return List.IndexOf(value)
   End Function 'IndexOf


   Public Sub Insert(index As Integer, value As Int16)
      List.Insert(index, value)
   End Sub 'Insert


   Public Sub Remove(value As Int16)
      List.Remove(value)
   End Sub 'Remove


   Public Function Contains(value As Int16) As Boolean 
      ' If value is not of type Int16, this will return false. 
      Return List.Contains(value)
   End Function 'Contains


   Protected Overrides Sub OnInsert(index As Integer, value As Object)
      ' Insert additional code to be run only when inserting values. 
   End Sub 'OnInsert


   Protected Overrides Sub OnRemove(index As Integer, value As Object)
      ' Insert additional code to be run only when removing values. 
   End Sub 'OnRemove


   Protected Overrides Sub OnSet(index As Integer, oldValue As Object, newValue As Object)
      ' Insert additional code to be run only when setting values. 
   End Sub 'OnSet


   Protected Overrides Sub OnValidate(value As Object)
      If Not GetType(System.Int16).IsAssignableFrom(value.GetType()) Then 
         Throw New ArgumentException("value must be of type Int16.", "value")
      End If 
   End Sub 'OnValidate 

End Class 'Int16Collection


Public Class SamplesCollectionBase

   Public Shared Sub Main()

      ' Creates and initializes a new CollectionBase. 
      Dim myI16 As New Int16Collection()

      ' Adds elements to the collection.
      myI16.Add( 1 )
      myI16.Add( 2 )
      myI16.Add( 3 )
      myI16.Add( 5 )
      myI16.Add( 7 )

      ' Display the contents of the collection using For Each. This is the preferred method.
      Console.WriteLine("Contents of the collection (using For Each):")
      PrintValues1(myI16)

      ' Display the contents of the collection using the enumerator.
      Console.WriteLine("Contents of the collection (using enumerator):")
      PrintValues2(myI16)

      ' Display the contents of the collection using the Count property and the Item property.
      Console.WriteLine("Initial contents of the collection (using Count and Item):")
      PrintIndexAndValues(myI16)

      ' Searches the collection with Contains and IndexOf.
      Console.WriteLine("Contains 3: {0}", myI16.Contains(3))
      Console.WriteLine("2 is at index {0}.", myI16.IndexOf(2))
      Console.WriteLine()

      ' Inserts an element into the collection at index 3.
      myI16.Insert(3, 13)
      Console.WriteLine("Contents of the collection after inserting at index 3:")
      PrintIndexAndValues(myI16)

      ' Gets and sets an element using the index.
      myI16(4) = 123
      Console.WriteLine("Contents of the collection after setting the element at index 4 to 123:")
      PrintIndexAndValues(myI16)

      ' Removes an element from the collection.
      myI16.Remove(2)

      ' Display the contents of the collection using the Count property and the Item property.
      Console.WriteLine("Contents of the collection after removing the element 2:")
      PrintIndexAndValues(myI16)

    End Sub 'Main


    ' Uses the Count property and the Item property. 
    Public Shared Sub PrintIndexAndValues(myCol As Int16Collection)
      Dim i As Integer 
      For i = 0 To myCol.Count - 1
          Console.WriteLine("   [{0}]:   {1}", i, myCol(i))
      Next i
      Console.WriteLine()
    End Sub 'PrintIndexAndValues


    ' Uses the For Each statement which hides the complexity of the enumerator. 
    ' NOTE: The For Each statement is the preferred way of enumerating the contents of a collection. 
    Public Shared Sub PrintValues1(myCol As Int16Collection)
      Dim i16 As Int16
      For Each i16 In  myCol
          Console.WriteLine("   {0}", i16)
      Next i16
      Console.WriteLine()
    End Sub 'PrintValues1


    ' Uses the enumerator.  
    ' NOTE: The For Each statement is the preferred way of enumerating the contents of a collection. 
    Public Shared Sub PrintValues2(myCol As Int16Collection)
      Dim myEnumerator As System.Collections.IEnumerator = myCol.GetEnumerator()
      While myEnumerator.MoveNext()
          Console.WriteLine("   {0}", myEnumerator.Current)
      End While
      Console.WriteLine()
    End Sub 'PrintValues2

End Class 'SamplesCollectionBase


'This code produces the following output. 

'Contents of the collection (using For Each): 
'   1 
'   2 
'   3 
'   5 
'   7 

'Contents of the collection (using enumerator): 
'   1 
'   2 
'   3 
'   5 
'   7 

'Initial contents of the collection (using Count and Item): 
'   [0]:   1 
'   [1]:   2 
'   [2]:   3 
'   [3]:   5 
'   [4]:   7 

'Contains 3: True 
'2 is at index 1. 

'Contents of the collection after inserting at index 3: 
'   [0]:   1 
'   [1]:   2 
'   [2]:   3 
'   [3]:   13 
'   [4]:   5 
'   [5]:   7 

'Contents of the collection after setting the element at index 4 to 123: 
'   [0]:   1 
'   [1]:   2 
'   [2]:   3 
'   [3]:   13 
'   [4]:   123 
'   [5]:   7 

'Contents of the collection after removing the element 2: 
'   [0]:   1 
'   [1]:   3 
'   [2]:   13 
'   [3]:   123 
'   [4]:   7

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft