home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Programmer'…arterly (Limited Edition) / Visual_Basic_Programmers_Journal_VB-CD_Quarterly_Limited_Edition_1995.iso / code / ch21code / topic.cls < prev    next >
Text File  |  1994-10-27  |  4KB  |  133 lines

  1. Version 1.0 Class
  2. Attribute VB_Name = "Topic"
  3. Attribute VB_Creatable = True
  4. Attribute VB_Exposed = True
  5. ' Topic class -- TOPIC.CLS
  6. '   Recursive data type -- stores any type of
  7. '   tree-structured information.
  8. '
  9. '   Properties
  10. '       Parent
  11. '       Index
  12. '       Value
  13. '
  14. '   Methods
  15. '       AddSubtopic
  16. '       Delete
  17. '       Topics
  18. '
  19. Option Explicit
  20.  
  21. ' Internal variables used to store information
  22. ' used by various methods and properties.
  23. Private mParent As Topic
  24. Private mIndex As Integer
  25. Private mValue As Variant
  26. Private colSubTopics As New Collection
  27.  
  28. ' Parent property (read always/write once).
  29. Public Property Get Parent() As Topic
  30.     ' If parent is not set, then the object
  31.     ' is the top-level topic, so Parent is Me.
  32.     If TypeName(mParent) = "Nothing" Then
  33.         Set Parent = Me
  34.     End If
  35.     ' Return the parent object.
  36.     Set Parent = mParent
  37. End Property
  38.  
  39. Public Property Set Parent(objSetting As Topic)
  40.     If TypeName(mParent) = "Nothing" Then
  41.         Set mParent = objSetting
  42.     Else
  43.         ' Can't reset.
  44.         Err.Raise 383, "Topic object", "Parent property is read-only."
  45.     End If
  46. End Property
  47.  
  48. ' Index property (read always/write once).
  49. Public Property Get Index() As Integer
  50.     ' Return the internal Index variable
  51.     ' intialized when Name is first set.
  52.     Index = mIndex
  53. End Property
  54.  
  55. Public Property Let Index(iSetting As Integer)
  56.     ' Check if property has already been set.
  57.     If mIndex = 0 Then
  58.         ' Set index on first call.
  59.         mIndex = iSetting
  60.     Else
  61.         ' Can't reset.
  62.         Err.Raise 383, "Topic object", "Index property is read-only."
  63.     End If
  64. End Property
  65.  
  66.  
  67. ' Value property (read/write).
  68. ' May contain object data or fundamental type
  69. ' (get/let/set).
  70. Public Property Get Value() As Variant
  71.     ' If the data is an object, use Set.
  72.     ' Otherwise, use regular assignment.
  73.     If IsObject(mValue) Then
  74.         ' Return the internal Data variable.
  75.         Set Value = mValue
  76.     Else
  77.         ' Return the internal Data variable.
  78.         Value = mValue
  79.     End If
  80. End Property
  81.  
  82. Public Property Let Value(vSetting As Variant)
  83.     ' Update the internal Data variable.
  84.     mValue = vSetting
  85. End Property
  86.  
  87. Public Property Set Value(objSetting As Object)
  88.     ' Update the internal Data variable.
  89.     Set mValue = objSetting
  90. End Property
  91.  
  92. ' AddSubTopic method.
  93. Public Function AddSubtopic() As Topic
  94.     ' Create a new subtopic.
  95.     Dim NewTopic As New Topic
  96.     ' Use a static Index to create a unique key
  97.     ' for each subtopic to add to the collection.
  98.     Static Index As Integer
  99.     Index = Index + 1
  100.     NewTopic.Index = Index
  101.     ' Set Parent property (creator is parent).
  102.     Set NewTopic.Parent = Me
  103.     ' Add the topic to the collection of
  104.     ' subtopics (NewTopic.Index is unique).
  105.     colSubTopics.Add NewTopic, Str(NewTopic.Index)
  106.     ' Return this object as the result of function.
  107.     Set AddSubtopic = NewTopic
  108. End Function
  109.  
  110. Public Function Topics() As Collection
  111.     Set Topics = colSubTopics
  112. End Function
  113.  
  114. Public Sub Delete()
  115.     ' If this is the top-level object, then
  116.     ' clear the collection, destroying all
  117.     ' subtopics.
  118.     If (Me Is Me.Parent) Then
  119.         ' Remove first item from the collection
  120.         ' until it is empty
  121.         Do Until colSubTopics.Count = 0
  122.             colSubTopics.Remove 1
  123.         Loop
  124.     ' This object isn't the first topic, so
  125.     ' remove it from its parent's collection.
  126.     Else
  127.         Me.Parent.Topics.Remove Me.Index
  128.         ' Subtopics under this topic are automatically
  129.         ' destroyed when they go out of scope.
  130.     End If
  131. End Sub
  132.  
  133.