Version 1.0 Class
Attribute VB_Name = "Topic"
Attribute VB_Creatable = True
Attribute VB_Exposed = True
' Topic class -- TOPIC.CLS
'   Recursive data type -- stores any type of
'   tree-structured information.
'
'   Properties
'       Parent
'       Index
'       Value
'
'   Methods
'       AddSubtopic
'       Delete
'       Topics
'
Option Explicit

' Internal variables used to store information
' used by various methods and properties.
Private mParent As Topic
Private mIndex As Integer
Private mValue As Variant
Private colSubTopics As New Collection

' Parent property (read always/write once).
Public Property Get Parent() As Topic
    ' If parent is not set, then the object
    ' is the top-level topic, so Parent is Me.
    If TypeName(mParent) = "Nothing" Then
        Set Parent = Me
    End If
    ' Return the parent object.
    Set Parent = mParent
End Property

Public Property Set Parent(objSetting As Topic)
    If TypeName(mParent) = "Nothing" Then
        Set mParent = objSetting
    Else
        ' Can't reset.
        Err.Raise 383, "Topic object", "Parent property is read-only."
    End If
End Property

' Index property (read always/write once).
Public Property Get Index() As Integer
    ' Return the internal Index variable
    ' intialized when Name is first set.
    Index = mIndex
End Property

Public Property Let Index(iSetting As Integer)
    ' Check if property has already been set.
    If mIndex = 0 Then
        ' Set index on first call.
        mIndex = iSetting
    Else
        ' Can't reset.
        Err.Raise 383, "Topic object", "Index property is read-only."
    End If
End Property


' Value property (read/write).
' May contain object data or fundamental type
' (get/let/set).
Public Property Get Value() As Variant
    ' If the data is an object, use Set.
    ' Otherwise, use regular assignment.
    If IsObject(mValue) Then
        ' Return the internal Data variable.
        Set Value = mValue
    Else
        ' Return the internal Data variable.
        Value = mValue
    End If
End Property

Public Property Let Value(vSetting As Variant)
    ' Update the internal Data variable.
    mValue = vSetting
End Property

Public Property Set Value(objSetting As Object)
    ' Update the internal Data variable.
    Set mValue = objSetting
End Property

' AddSubTopic method.
Public Function AddSubtopic() As Topic
    ' Create a new subtopic.
    Dim NewTopic As New Topic
    ' Use a static Index to create a unique key
    ' for each subtopic to add to the collection.
    Static Index As Integer
    Index = Index + 1
    NewTopic.Index = Index
    ' Set Parent property (creator is parent).
    Set NewTopic.Parent = Me
    ' Add the topic to the collection of
    ' subtopics (NewTopic.Index is unique).
    colSubTopics.Add NewTopic, Str(NewTopic.Index)
    ' Return this object as the result of function.
    Set AddSubtopic = NewTopic
End Function

Public Function Topics() As Collection
    Set Topics = colSubTopics
End Function

Public Sub Delete()
    ' If this is the top-level object, then
    ' clear the collection, destroying all
    ' subtopics.
    If (Me Is Me.Parent) Then
        ' Remove first item from the collection
        ' until it is empty
        Do Until colSubTopics.Count = 0
            colSubTopics.Remove 1
        Loop
    ' This object isn't the first topic, so
    ' remove it from its parent's collection.
    Else
        Me.Parent.Topics.Remove Me.Index
        ' Subtopics under this topic are automatically
        ' destroyed when they go out of scope.
    End If
End Sub

