Warning: This will be some clumsy text, because I am struggling hard to find the English words for what I am trying to write.
I have written a desktop application for taking notes and categorizing them. This application includes an undo function for deleted notes; whenever you delete a note, it saves a copy of it internally (as long as the application is running), and when you undo the deletion the deleted note will be copied back.
I could have done that with an array of notes easily (and did so at first), but since I am still rather a beginner and working to grasp basic programming concepts I thought I’d rather use a stack data structure instead of an array. The stack instances should only hold one data type, but the class should be flexible enough to instantiate stacks for multiple data types. (I thought of Java’s generic classes, which are not available in Xojo, I know.)
So I tried to mimic a stack class I once wrote in Java; the stack node is a different class, since Xojo doesn’t allow inner classes. In order to manage the balancing between type safety and flexibility I chose this way: The stack nodes’ content is always a variant, so you can stuff any data in it you like. But when the first stack node is added to the stack, the stack “saves” the data type and allows only nodes of the same data type to be added.
For the moment the class works well in my project.
My question to more experienced programmers is, if the following code seems to be a valid approach to a flexible stack class for you:
[code]Class StackNode
Methods
Sub Constructor(PContent As Variant)
Content = PContent
NextNode = Nil
DataType = Content.Type ClassName = “”
If DataType = Variant.TypeObject Then
Dim t As Introspection.TypeInfo = Introspection.GetType(Content.ObjectValue)
ClassName = t.Name
End If
End Sub
Function GetClassName() As String
Return ClassName
End Function
Function GetContent() As Variant
Return Content
End Function
Function GetDataType() As Integer
Return DataType
End Function
Function GetNext() As StackNode
Return NextNode
End Function
Sub SetNext(node As StackNode)
NextNode = node
End Sub
Properties
Private ClassName As String
Private Content As Variant
Private DataType As Integer
Private NextNode As StackNode
End Class[/code]
[code]Class Stack
Methods
Sub Constructor()
Head = Nil
End Sub
Function IsEmpty() As Boolean Return
Head = Nil
End Function
Sub Pop()
If Not IsEmpty Then
Head = Head.GetNext
End If
End Sub
Sub Push(Content As Variant)
If Content <> Nil Then
If IsEmpty Then
DataType = Content.Type
If DataType = Variant.TypeObject Then
Dim t As Introspection.TypeInfo = Introspection.GetType(Content.ObjectValue)
ClassName = t.Name
End If
End If
Dim node As StackNode = New StackNode(Content)
If node.GetDataType = DataType And _
(node.GetDataType <> Variant.TypeObject Or _
(node.GetDataType = Variant.TypeObject And node.GetClassName = ClassName)) Then
node.SetNext(Head)
Head = node
End If
End If
End Sub
Function Top() As Variant
If Not IsEmpty Then
Return Head.GetContent
Else
Return Nil
End If
End Function
Properties
Private ClassName As String
Private DataType As Integer
Private Head As StackNode
End Class[/code]