Create a fixed-size wrapper around an ArrayList.
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic
Public Class SamplesArrayList
Public Shared Sub Main()
Dim myAL As New ArrayList()
myAL.Add("The")
myAL.Add("quick")
myAL.Add("brown")
myAL.Add("fox")
Dim myFixedSizeAL As ArrayList = ArrayList.FixedSize(myAL)
Dim msg As String
If myAL.IsFixedSize Then
msg = "has a fixed size"
Else
msg = "does not have a fixed size"
End If
PrintValues(myAL, " "c)
PrintValues(myFixedSizeAL, " "c)
myFixedSizeAL.Sort()
PrintValues(myAL, " "c)
PrintValues(myFixedSizeAL, " "c)
myFixedSizeAL.Reverse()
PrintValues(myAL, " "c)
PrintValues(myFixedSizeAL, " "c)
myAL.Add("AddMe")
PrintValues(myAL, " "c)
PrintValues(myFixedSizeAL, " "c)
Try
myFixedSizeAL.Add("AddMe2")
Catch myException As Exception
Console.WriteLine("Exception: " + myException.ToString())
End Try
Try
myFixedSizeAL.Insert(3, "InsertMe")
Catch myException As Exception
Console.WriteLine("Exception: " + myException.ToString())
End Try
End Sub 'Main
Public Shared Sub PrintValues(myList As IEnumerable, mySeparator As Char)
Dim obj As [Object]
For Each obj In myList
Console.Write("{0}{1}", mySeparator, obj)
Next obj
Console.WriteLine()
End Sub 'PrintValues
End Class
Related examples in the same category