StringList Class
Category TechnicalThe other day I posted some code for a class I was working on: Class StringList. I have added some additional functionality, and cleaned up the class a bit. Here is the new StringList Class:
' Option Public INTENTIONALLY REMOVED
Option Explicit
Use "Utilities"
Public Class StringList
%REM
This class is 256 element array of strings.
The properties and methods allow easy navigation of the array.
%END REM
Private bool_Initialized As Boolean
Private intElements As Integer
Private byteCurrentPos As Byte
Private byteUbound As Byte
Private varElements() As String
' INTERFACE
' PROPERTIES
Public Property Get HasElements As Boolean
HasElements = (intElements% > 0)
End Property ' Get HasElements
Public Property Get ElementCount As Integer
ElementCount% = intElements%
End Property ' Get ElementCount
Public Property Get CurrentElementNumber As Integer
CurrentElementNumber% = Cint(byteCurrentPos) + 1
End Property ' CurrentElementNumber
' METHODS
Public Sub New
Call Initialize_Class
End Sub ' New
Public Sub Initialize_Class
Redim varElements(0 To 255) As String
intElements% = 0
byteCurrentPos = 0
byteUbound = 0
bool_Initialized = True
End Sub ' Initialize_Class
Public Function AddElement (strSource As String) As Boolean
' Returns True if the element is added to the StringList, returns False otherwise.
On Error Goto ErrorTrap
If (intElements% < 256) Then
If (intElements% <> 0) Then incValue byteUbound, 1
varElements(byteUbound) = strSource$
byteCurrentPos = byteUbound
incValue intElements%, 1
AddElement = True
Else
AddElement = False
End If ' (intElements% < 256)
ExitPoint:
Exit Function
ErrorTrap:
AddElement = False
Resume ExitPoint
End Function ' AddElement
Public Function GetFirst As String
' returns the first element of the StringList
GetFirst = varElements(0)
byteCurrentPos = 0
End Function ' GetFirst
Public Function GetNext As String
' returns the next elment (based on the current position) of the StringList
If (byteCurrentPos < byteUbound) Then
incValue byteCurrentPos, 1
GetNext = varElements(byteCurrentPos)
Else
GetNext = ""
End If ' (byteCurrentPos < byteUbound)
End Function ' GetNext
Public Function GetPrevious As String
' returns the previous elment (based on the current position) of the StringList
If (byteCurrentPos > 0) Then
incValue byteCurrentPos, -1
GetPrevious = varElements(byteCurrentPos)
Else
GetPrevious = ""
End If ' (byteCurrentPos > 0)
End Function ' GetPrevious
Public Function GetLast As String
' returns the last element of the StringList
byteCurrentPos = byteUbound
GetLast = varElements(byteCurrentPos)
End Function ' GetLast
Public Function OrdEntry (bytePos As Byte) As String
' returns the entry at the psuedo ordinal position
If Me.HasElements Then
If (bytePos < intElements) Then
OrdEntry = varElements(bytePos)
byteCurrentPos = bytePos
Else
OrdEntry = ""
End If ' (bytePos < intElements)
Else
OrdEntry = ""
End If ' Me.HasElements
End Function ' OrdEntry
Public Function PopLast As String
' This function is similar to GetLast,
' except it also removes the last element from the StringList
PopLast = varElements(byteUbound)
If (intElements% > 0) Then
varElements(byteUbound) = ""
incValue intElements%, -1
End If ' (intElements% > 0)
If byteUbound > 0 Then incValue byteUbound, -1
byteCurrentPos = byteUbound
End Function ' PopLast
Public Function PopFirst As String
' This function is similar to GetFirst,
' except it also removes the first element from the StringList and then reorders the StringList
If (intElements% > 0) Then
PopFirst = varElements(0)
If (byteUbound > 0) Then
For byteCurrentPos = 1 To byteUbound
varElements(byteCurrentPos -1) = varElements(byteCurrentPos)
Next ' byteCurrentPos
varElements(byteUbound) = ""
incValue byteUbound, -1
Else
varElements(0) = ""
End If ' (byteUbound > 0)
incValue intElements%, -1
Else
PopFirst = ""
End If ' (intElements% > 0)
byteCurrentPos = 0
End Function ' PopFirst
Public Sub Delete
If bool_Initialized Then
Redim varElements(0) As String
varElements(0) = ""
End If ' bool_Initialized
intElements% = 0
byteCurrentPos = 0
byteUbound = 0
bool_Initialized = False
End Sub ' Delete
End Class ' StringList
Option Explicit
Use "Utilities"
Public Class StringList
%REM
This class is 256 element array of strings.
The properties and methods allow easy navigation of the array.
%END REM
Private bool_Initialized As Boolean
Private intElements As Integer
Private byteCurrentPos As Byte
Private byteUbound As Byte
Private varElements() As String
' INTERFACE
' PROPERTIES
Public Property Get HasElements As Boolean
HasElements = (intElements% > 0)
End Property ' Get HasElements
Public Property Get ElementCount As Integer
ElementCount% = intElements%
End Property ' Get ElementCount
Public Property Get CurrentElementNumber As Integer
CurrentElementNumber% = Cint(byteCurrentPos) + 1
End Property ' CurrentElementNumber
' METHODS
Public Sub New
Call Initialize_Class
End Sub ' New
Public Sub Initialize_Class
Redim varElements(0 To 255) As String
intElements% = 0
byteCurrentPos = 0
byteUbound = 0
bool_Initialized = True
End Sub ' Initialize_Class
Public Function AddElement (strSource As String) As Boolean
' Returns True if the element is added to the StringList, returns False otherwise.
On Error Goto ErrorTrap
If (intElements% < 256) Then
If (intElements% <> 0) Then incValue byteUbound, 1
varElements(byteUbound) = strSource$
byteCurrentPos = byteUbound
incValue intElements%, 1
AddElement = True
Else
AddElement = False
End If ' (intElements% < 256)
ExitPoint:
Exit Function
ErrorTrap:
AddElement = False
Resume ExitPoint
End Function ' AddElement
Public Function GetFirst As String
' returns the first element of the StringList
GetFirst = varElements(0)
byteCurrentPos = 0
End Function ' GetFirst
Public Function GetNext As String
' returns the next elment (based on the current position) of the StringList
If (byteCurrentPos < byteUbound) Then
incValue byteCurrentPos, 1
GetNext = varElements(byteCurrentPos)
Else
GetNext = ""
End If ' (byteCurrentPos < byteUbound)
End Function ' GetNext
Public Function GetPrevious As String
' returns the previous elment (based on the current position) of the StringList
If (byteCurrentPos > 0) Then
incValue byteCurrentPos, -1
GetPrevious = varElements(byteCurrentPos)
Else
GetPrevious = ""
End If ' (byteCurrentPos > 0)
End Function ' GetPrevious
Public Function GetLast As String
' returns the last element of the StringList
byteCurrentPos = byteUbound
GetLast = varElements(byteCurrentPos)
End Function ' GetLast
Public Function OrdEntry (bytePos As Byte) As String
' returns the entry at the psuedo ordinal position
If Me.HasElements Then
If (bytePos < intElements) Then
OrdEntry = varElements(bytePos)
byteCurrentPos = bytePos
Else
OrdEntry = ""
End If ' (bytePos < intElements)
Else
OrdEntry = ""
End If ' Me.HasElements
End Function ' OrdEntry
Public Function PopLast As String
' This function is similar to GetLast,
' except it also removes the last element from the StringList
PopLast = varElements(byteUbound)
If (intElements% > 0) Then
varElements(byteUbound) = ""
incValue intElements%, -1
End If ' (intElements% > 0)
If byteUbound > 0 Then incValue byteUbound, -1
byteCurrentPos = byteUbound
End Function ' PopLast
Public Function PopFirst As String
' This function is similar to GetFirst,
' except it also removes the first element from the StringList and then reorders the StringList
If (intElements% > 0) Then
PopFirst = varElements(0)
If (byteUbound > 0) Then
For byteCurrentPos = 1 To byteUbound
varElements(byteCurrentPos -1) = varElements(byteCurrentPos)
Next ' byteCurrentPos
varElements(byteUbound) = ""
incValue byteUbound, -1
Else
varElements(0) = ""
End If ' (byteUbound > 0)
incValue intElements%, -1
Else
PopFirst = ""
End If ' (intElements% > 0)
byteCurrentPos = 0
End Function ' PopFirst
Public Sub Delete
If bool_Initialized Then
Redim varElements(0) As String
varElements(0) = ""
End If ' bool_Initialized
intElements% = 0
byteCurrentPos = 0
byteUbound = 0
bool_Initialized = False
End Sub ' Delete
End Class ' StringList
Hope this helps!
-Devin

The Pridelands
Chris Byrne
Show n' Tell Thursdays


