| Answer | The vector class allows enumeration. The HASH table class uses lists to store the data.
'VECTOR Class
Option Public
Option Declare
Use "HashTable.Class"
' Created by Steve Robinson - Appsworks 09/10/2000
'www.appsworks.com
'steve.robinson@appsworks.com
Class Vector
Private p_oHashTable As HashTable
Private p_idx As Integer
Private Cursor As Integer
Private enum_pointer As Integer
Property Get hasMoreElements As Integer
If enum_pointer < p_idx Then
hasMoreElements = True
Else
hasMoreElements = False
End If
End Property
Sub new()
Dim count As Integer
Set p_oHashTable = New Hashtable
p_idx=0
enum_pointer = 0
End Sub
Public Function AddElement(obj As Variant)
cursor=p_idx
Call p_oHashTable.addElement(Cstr(p_idx),obj)
p_idx=p_idx+1
End Function
'AddElementAt(position as integer,obj as variant)
'RemoveElementAt(position as integer)
'GetElementAt(position as integer) as variant
'########### Enumeration methods ##################
Function getElement As Variant
'Set getElement=p_oHashTable.GetElement(Cstr(enum_pointer))
getElement=p_oHashTable.GetElement(Cstr(enum_pointer))
'Msgbox enum_pointer
enum_pointer = enum_pointer + 1
End Function
Public Sub resetEnumeration
Me.enum_pointer = 0
End Sub
'########### END Enumeration methods ##############
Function getFirstElement As Variant
Cursor=0
getFirstElement=p_oHashTable.GetElement(Cstr(Cursor))
End Function
Function getLastElement As Variant
Cursor=p_idx-1
getLastElement=p_oHashTable.GetElement(Cstr(Cursor))
End Function
Function getNextElement As Variant
Cursor=Cursor+1
getNextElement=p_oHashTable.GetElement(Cstr(Cursor))
End Function
Function getPreviousElement As Variant
Cursor=Cursor-1
getPreviousElement=p_oHashTable.GetElement(Cstr(Cursor))
End Function
Function getCount As Integer
getCount=p_idx
End Function
End Class
'HASH TABLEclass:
Class HashTable
Private p_List_HashTable List As Variant
Property Get getList As Variant
getList=p_List_Hashtable
End Property
Public Function hasElement(id As String) As Integer
'stack.push "(HashTable..hasElement)"
hasElement = Iselement( Me.p_List_HashTable( Lcase( id ) ) )
'stack.pop "(HashTable..hasElement)"
End Function
Public Function getElement(id As String) As Variant
'stack.push "(HashTable..getElement)"
If Not id ="" Then
If Iselement( Me.p_List_HashTable( Lcase( id ) ) ) Then
If Isobject(Me.p_List_HashTable( Lcase( id ) ) ) Then
Set getElement = Me.p_List_HashTable( Lcase( id ) )
Else
getElement = Me.p_List_HashTable( Lcase(id) )
End If
Else
Set getElement = Nothing
End If
End If
'stack.pop "(HashTable..getElement)"
End Function
Public Function addElement(id As String, obj As Variant) As Variant
'Returns the object added to the HashTable or nothing if it was not added
'stack.push "(HashTable..addElement)"
If Not id ="" Then
If Isobject( obj ) Then
Set Me.p_List_HashTable( Lcase( id ) ) = obj
Set addElement = Me.p_List_HashTable( Lcase( id ) )
Else
Me.p_List_HashTable( Lcase( id ) ) = obj
addElement = Me.p_List_HashTable( Lcase( id ) )
End If
Else
Set addElement = Nothing
End If
'stack.pop "(HashTable..addElement)"
End Function
Function removeElement(id As String) As Variant
'Returns the object removed from the HashTable or nothing if not present
'stack.push "(HashTable..removeElement)"
If Not id ="" Then
If Iselement( Me.p_List_HashTable( Lcase( id ) ) ) Then
'First we get the element so we can return it
If Isobject(Me.p_List_HashTable( Lcase( id ) ) ) Then
Set removeElement = Me.p_List_HashTable( Lcase( id ) )
Else
removeElement = Me.p_List_HashTable( Lcase(id) )
End If
'now we remove the element
Erase Me.p_List_HashTable( Lcase(id) )
Else
'no element was found
Set removeElement = Nothing
End If
End If
'stack.pop "(HashTable..removeElement)"
End Function
Public Function close() As Integer
Forall e In Me.p_List_HashTable
Set e = Nothing
End Forall
End Function
Sub delete
Call Me.close()
End Sub
End Class
'TEST
Use "vector.class"
Dim v As New Vector
Dim element As Variant
v.addElement("This is the bit of string data: A number follows")
v.addElement(1001)
v.addElement("This is the bit of string data: A number follows")
v.addElement(1002)
While v.hasMoreElements
element=v.getElement
Msgbox element
Wend
element=v.getFirstElement
Msgbox "GetFirstElement=" & element
element=v.getNextElement
Msgbox "GetNextElement=" & element
element=v.getLastElement
Msgbox "GetLastElement=" & element |