Module Module1

    Sub Main()

        ' The strength of the ArrayList is its ability to
        ' expand in size (elements) as needed and provides
        ' tools to manage its 'elasticity'.  Remember:
        ' this comes with a price ... The reallocation
        ' of the ArrayList could be expensive if over-used.
        '
        ' Also, it is LESS strongly-typed than an array, 
        ' allowing different objects to be stored in it
        ' (Basically the equivilent of creating an Array
        ' of Objects)
        '
        ' There are other methods and properties that are
        ' the same as the Array, such as:
        '    - Clear
        '    - Reverse
        '    - IndexOf, LastIndexOf
        '    - Count
        '    - Clone
        '    - GetType
        ' Please see the IList: Array video for more details
        ' about these.


        Dim car1 As Car = New Car("Dodge", "Viper", 2002)
        Dim car2 As Car = New Car("Jaguar", "XJ8", 2001)
        Dim car3 As Car = New Car("Mercedes", "SL55", 2003)

        Dim Cars As ArrayList = New ArrayList(5)
        Cars.Add(car1)
        Cars.Add(car2)
        Cars.Add(car3)

        ' Determine capacity of the arraylist
        '   - Capacity
        '   - TrimToSize 
        Console.Write("Number of car objects that the ArrayList can hold: " & _
            Cars.Capacity.ToString() & vbCrLf)

        Cars.TrimToSize()
        Console.Write("Number AFTER ArrayList is Trimmed: " & _
            Cars.Capacity.ToString() & vbCrLf)


        'Display a single item
        '   - Item
        Console.Write("The second car is a:" & Cars.Item(1).Make & _
            " - " & Cars.Item(1).Model & " (" & Cars.Item(1).Year.ToString() & vbCrLf)


        ' Check for existence of an object in the collection
        '   - Contains 
        Console.Write("Number of cars: " & Cars.Count.ToString() & vbCrLf)
        Dim bExists As Boolean = Cars.Contains(car3)

        Console.Write("The car was found in the Array: " & _
            bExists.ToString() & vbCrLf)

        ' Remove the object from the collection, check again
        '   - Remove 
        Cars.Remove(car3)
        bExists = Cars.Contains(car3)
        Console.Write("Number of cars AFTER car3 is removed: " & _
            Cars.Count.ToString() & vbCrLf)

        Console.Write("The car was found in the Array: " & _
            bExists.ToString() & vbCrLf)


        ' Use the static ArrayList type to create another
        ' version (a wrapper version) of our ArrayList instance
        ' that is Fixed in Size and Read only.  
        '   - FixedSize
        '   - IsFixedSize
        '   - ReadOnly
        '   - IsReadOnly 

        Dim fixedCars As ArrayList = ArrayList.FixedSize(Cars)
        If fixedCars.IsFixedSize Then
            Console.Write("The fixedCars is fixed in size!" & vbCrLf)
        End If

        Dim readonlyCars As ArrayList = ArrayList.ReadOnly(Cars)
        If readonlyCars.IsReadOnly Then
            Console.Write("The readonlyCars is READ ONLY!" & vbCrLf)
        End If

        ' ToArray allows you to copy the objects down to a regular array
        ' Notice that the new array must be typed as an Object!
        '   - ToArray
        Dim Autos(Cars.Count) As Object
        Autos = Cars.ToArray()

        Dim oCar As Car
        For Each oCar In Autos
            Console.Write("Car in array: " & oCar.Make & vbCrLf)
        Next


        ' -------------------------------------------------
        ' ArrayLists provide more flexibility, allowing different
        ' types to be added to the same structure.
        '---------------------------------------------------
        Dim arList As New ArrayList()
        arList.Add(car1)
        arList.Add(New Truck("Ford", "F-10", 2002, 15))

        Dim oVehicle As Object
        For Each oVehicle In arList
            Select Case oVehicle.GetType.Name.ToString()
                Case "Car"
                    Console.Write("CAR" & vbCrLf)
                Case "Truck"
                    Console.Write("TRUCK" & vbCrLf)
            End Select
        Next



    End Sub


    Class Car

        Public Make As String
        Public Model As String
        Public Year As Integer

        Public Sub New(ByVal xMake As String, ByVal xModel As String, ByVal xYear As Integer)

            Make = xMake
            Model = xModel
            Year = xYear

        End Sub

    End Class

    Class Truck

        Public Make As String
        Public Model As String
        Public Year As Integer
        Public TowingCapacity As Integer

        Public Sub New(ByVal xMake As String, ByVal xModel As String, ByVal xYear As Integer, ByVal xTowingCapacity As Integer)

            Make = xMake
            Model = xModel
            Year = xYear
            TowingCapacity = xTowingCapacity

        End Sub

    End Class

End Module
