Export (0) Print
Expand All

Collection(Of T).InsertItem Method

Inserts an element into the Collection(Of T) at the specified index.

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

'Declaration
Protected Overridable Sub InsertItem ( _
	index As Integer, _
	item As T _
)

Parameters

index
Type: System.Int32
The zero-based index at which item should be inserted.
item
Type: T
The object to insert. The value can be Nothing for reference types.

ExceptionCondition
ArgumentOutOfRangeException

index is less than zero.

-or-

index is greater than Count.

Collection(Of T) accepts Nothing as a valid value for reference types and allows duplicate elements.

If index is equal to Count, item is added to the end of Collection(Of T).

This method is an O(n) operation, where n is Count.

The following code example shows how to derive a collection class from a constructed type of the Collection(Of T) generic class, and how to override the protected InsertItem, RemoveItem, ClearItems, and SetItem methods to provide custom behavior for the Add, Insert, Remove, and Clear methods, and for setting the Item property.

The custom behavior provided by this example is a Changed notification event that is raised at the end of each of the protected methods. The Dinosaurs class inherits Collection<string> (Collection(Of String) in Visual Basic) and defines the Changed event, which uses a DinosaursChangedEventArgs class for the event information, and an enumeration to identify the kind of change.

The code example calls several properties and methods of Collection(Of T) to demonstrate the custom event.


Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.Windows.Controls

Public Class Dinosaurs
    Inherits Collection(Of String)

    Public Event Changed As EventHandler(Of DinosaursChangedEventArgs)

    Protected Overrides Sub InsertItem( _
        ByVal index As Integer, ByVal newItem As String)

        MyBase.InsertItem(index, newItem)

        RaiseEvent Changed(Me, New DinosaursChangedEventArgs( _
            ChangeType.Added, newItem, Nothing))
    End Sub

    Protected Overrides Sub SetItem(ByVal index As Integer, _
        ByVal newItem As String)

        Dim replaced As String = Items(index)
        MyBase.SetItem(index, newItem)

        RaiseEvent Changed(Me, New DinosaursChangedEventArgs( _
            ChangeType.Replaced, replaced, newItem))
    End Sub

    Protected Overrides Sub RemoveItem(ByVal index As Integer)

        Dim removedItem As String = Items(index)
        MyBase.RemoveItem(index)

        RaiseEvent Changed(Me, New DinosaursChangedEventArgs( _
            ChangeType.Removed, removedItem, Nothing))
    End Sub

    Protected Overrides Sub ClearItems()
        MyBase.ClearItems()

        RaiseEvent Changed(Me, New DinosaursChangedEventArgs( _
            ChangeType.Cleared, Nothing, Nothing))
    End Sub

End Class

' Event argument for the Changed event.
'
Public Class DinosaursChangedEventArgs
    Inherits EventArgs

    Public ReadOnly ChangedItem As String
    Public ReadOnly ChangeType As ChangeType
    Public ReadOnly ReplacedWith As String

    Public Sub New(ByVal change As ChangeType, ByVal item As String, _
        ByVal replacement As String)

        ChangeType = change
        ChangedItem = item
        ReplacedWith = replacement
    End Sub
End Class

Public Enum ChangeType
    Added
    Removed
    Replaced
    Cleared
End Enum

Public Class Example

    Private Shared _TextBlock As TextBlock

    Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

        Example._TextBlock = outputBlock

        Dim dinosaurs As New Dinosaurs

        AddHandler dinosaurs.Changed, AddressOf ChangedHandler

        dinosaurs.Add("Psitticosaurus")
        dinosaurs.Add("Caudipteryx")
        dinosaurs.Add("Compsognathus")
        dinosaurs.Add("Muttaburrasaurus")

        Display(outputBlock, dinosaurs)

        outputBlock.Text &= String.Format(vbLf & "IndexOf(""Muttaburrasaurus""): {0}" & vbCrLf, _
            dinosaurs.IndexOf("Muttaburrasaurus"))

        outputBlock.Text &= String.Format(vbLf & "Contains(""Caudipteryx""): {0}" & vbCrLf, _
            dinosaurs.Contains("Caudipteryx"))

        outputBlock.Text &= String.Format(vbLf & "Insert(2, ""Nanotyrannus"")") & vbCrLf
        dinosaurs.Insert(2, "Nanotyrannus")

        outputBlock.Text &= String.Format(vbLf & "dinosaurs(2): {0}" & vbCrLf, dinosaurs(2))

        outputBlock.Text &= vbLf & "dinosaurs(2) = ""Microraptor""" & vbCrLf
        dinosaurs(2) = "Microraptor"

        outputBlock.Text &= vbLf & "Remove(""Microraptor"")" & vbCrLf
        dinosaurs.Remove("Microraptor")

        outputBlock.Text &= vbLf & "RemoveAt(0)" & vbCrLf
        dinosaurs.RemoveAt(0)

        Display(outputBlock, dinosaurs)

    End Sub

    Private Shared Sub Display(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal cs As Collection(Of String))
        _TextBlock.Text &= vbCrLf
        For Each item As String In cs
            _TextBlock.Text &= item & vbCrLf
        Next item
    End Sub

    Private Shared Sub ChangedHandler(ByVal source As Object, _
        ByVal e As DinosaursChangedEventArgs)

        If e.ChangeType = ChangeType.Replaced Then
            _TextBlock.Text &= String.Format("{0} was replaced with {1}", _
                e.ChangedItem, e.ReplacedWith) & vbCrLf

        ElseIf e.ChangeType = ChangeType.Cleared Then
            _TextBlock.Text &= "The dinosaur list was cleared." & vbCrLf

        Else
            _TextBlock.Text &= String.Format("{0} was {1}.", _
                e.ChangedItem, e.ChangeType) & vbCrLf
        End If
    End Sub

End Class
' This code example produces the following output:
'
'Psitticosaurus was Added.
'Caudipteryx was Added.
'Compsognathus was Added.
'Muttaburrasaurus was Added.
'
'Psitticosaurus
'Caudipteryx
'Compsognathus
'Muttaburrasaurus
'
'IndexOf("Muttaburrasaurus"): 3
'
'Contains("Caudipteryx"): True
'
'Insert(2, "Nanotyrannus")
'Nanotyrannus was Added.
'
'dinosaurs(2): Nanotyrannus
'
'dinosaurs(2) = "Microraptor"
'Nanotyrannus was replaced with Microraptor
'
'Remove("Microraptor")
'Microraptor was Removed.
'
'RemoveAt(0)
'Psitticosaurus was Removed.
'
'Caudipteryx
'Compsognathus
'Muttaburrasaurus


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show:
© 2014 Microsoft