' D. de Haas
' Amsterdam, Netherlands
' 29 October 2002
' ddhadam@yahoo.com

Option Explicit On 
Option Strict On

Imports System
Imports System.Drawing

Namespace Graphics

    Public Class BlockCollection
        ' Specialized Collection for storing DdH.Graphics.Block
        ' It's is actually an Array, that acts like an collection
        ' so it's possible to place blocks at any index

        Private mBlocks() As Graphics.Block
        Private mCount As Integer

        '==== Events

        '==== Constructors

        '==== Private Methods/Functions

        '==== Public Functions

        Public Overridable Function Add(ByVal block As Graphics.Block) As Integer
            ' Add a block the collection

            ' Set the upper bound of the inner array
            If mBlocks Is Nothing Then
                ReDim mBlocks(0)
            Else
                ReDim Preserve mBlocks(mBlocks.Length)
            End If

            Dim index As Integer
            index = mBlocks.Length - 1

            ' Add block to the inner array
            mBlocks(index) = block

            Return index
        End Function

        '==== Public Methodes

        Public Overridable Sub Clear()
            ' Clears the collection
            mBlocks = Nothing
        End Sub

        Public Overridable Sub Insert(ByVal index As Integer, ByVal block As Graphics.Block)
            ' Add a block the collection on the (index) location

            ' Set the upper bound of the inner array
            If index >= Me.Count Then
                ReDim Preserve mBlocks(index)
            End If

            ' Add block to the inner array
            mBlocks(index) = block
        End Sub

        Public Overridable Sub Remove(ByVal index As Integer)
            ' Remove block from the collection

            Try
                mBlocks(index) = Nothing
            Catch
                ' nothing
            End Try
        End Sub

        '==== Properties

        Public ReadOnly Property Item(ByVal index As Integer) As Block
            Get
                Try
                    Return mBlocks(index)
                Catch
                    Return Nothing
                End Try
            End Get
        End Property

        Public ReadOnly Property Count() As Integer
            Get
                If mBlocks Is Nothing Then
                    Return 0
                Else
                    Return mBlocks.Length
                End If
            End Get
        End Property

    End Class

End Namespace
