tags:

views:

12

answers:

0

I have made a DataTree class that has Name, Value, and Items properties. It is designed to function like PHP's associative multi-dimensional arrays. Everything works except setting the Item property. The item property excepts one or more parameters. Each represents the name property of a DataTree instance. This would be easy to do if I could have a variable that references a DataTree inside of a DataTree. If not, how should I go about doing this?

Here is the code for the DataTree and DataTreeCollection classes:

Public Class DataTree
    Property Name As String

    Private _Value As String
    Property Value As String
        Get
            Return _Value
        End Get
        Set(ByVal value As String)
            _Value = value
            _Items = Nothing
        End Set
    End Property

    Private _Items As DataTreeCollection
    Property Items As DataTreeCollection
        Get
            Return _Items
        End Get
        Set(ByVal value As DataTreeCollection)
            _Items = value
            _Value = Nothing
        End Set
    End Property

    Default Property Item(ByVal name1 As String, ByVal ParamArray names() As String) As String
        Get
            Dim dt As DataTree = Items(0)

            If Not dt.Name = name1 Then
                dt = dt.Items.ItemByName(name1)
            End If

            For Each name As Integer In names
                dt = dt.Items.ItemByName(name)
            Next

            Return dt.Value
        End Get
        Set(ByVal value As String)
            Dim dt As DataTree = Items(0)

            If Not dt.Name = name1 Then
                dt = dt.Items.ItemByName(name1)
            End If

            For Each name As Integer In names
                dt = dt.Items.ItemByName(name)
            Next

            dt.Value = value
        End Set
    End Property

    Private Sub New()
    End Sub

    Public Overloads Shared Function Neo(ByVal name As String) As DataTree
        Dim dt As New DataTree

        dt._Name = name

        Return dt
    End Function
    Public Overloads Shared Function Neo(ByVal name As String, ByVal value As String) As DataTree
        Dim dt As New DataTree

        dt._Name = name
        dt.Value = value

        Return dt
    End Function
    Public Overloads Shared Function Neo(ByVal name As String, ByVal ParamArray values() As String) As DataTree
        Dim dt As New DataTree
        Dim dtc As New DataTreeCollection
        Dim i As Integer = 0

        dt._Name = name
        For Each value As String In values
            dtc.Add(DataTree.Neo(i, value))
            i += 1
        Next
        dt.Items = dtc

        Return dt
    End Function
    Public Overloads Shared Function Neo(ByVal name As String, ByVal ParamArray values() As DataTree) As DataTree
        Dim dt As New DataTree
        Dim dtc As New DataTreeCollection
        Dim i As Integer = 0

        dt._Name = name
        For Each value As DataTree In values
            dtc.Add(value)
            i += 1
        Next
        dt.Items = dtc

        Return dt
    End Function
    Public Overloads Shared Function Neo(ByVal name As String, ByVal values As DataTreeCollection) As DataTree
        Dim dt As New DataTree
        Dim i As Integer = 0

        dt._Name = name
        dt.Items = values

        Return dt
    End Function
End Class

Public Class DataTreeCollection
    Inherits CollectionBase

    Public Sub Add(ByVal value As DataTree)
        List.Add(value)
    End Sub

    Public Function Contains(ByVal value As DataTree) As Boolean
        Return List.Contains(value)
    End Function

    Public Function IndexOf(ByVal value As DataTree) As Integer
        Return List.IndexOf(value)
    End Function

    Public Sub Insert(ByVal index As Integer, ByVal value As DataTree)
        List.Insert(index, value)
    End Sub

    Default Public ReadOnly Property Item(ByVal index As Integer) As DataTree
        Get
            Return DirectCast(List(index), DataTree)
        End Get
    End Property

    Public ReadOnly Property ItemByName(ByVal name As String) As DataTree
        Get
            For Each obj As Object In List
                Dim dt As DataTree = DirectCast(obj, DataTree)

                If dt.Name = name Then
                    Return dt
                End If
            Next
            Return Nothing
        End Get
    End Property

    Public Sub Remove(ByVal value As DataTree)
        List.Remove(value)
    End Sub
End Class