Library code snippets
String container (mutant class, really)
By Matej Kasper, published on 03 Jun 2006
String container (mutant class, really)
Look In code, I don't suppose, that some more explaining than XML comments is needed
Briefly, it has same item working mechanism as e.g. ListBox... and also it 'builtin-cooperates' with listbox. Also added field logical-like operations And, Or, Xor, AndNot, which enables you to compare, or merge etc your string/integer containers...
''' <summary>Container of integer or string values. Do not store chr(0) and chr(1) in items.</summary>
Public Class StrContainer
Dim Data As String
Dim Cnt As Integer
Dim chr0 As Char = Chr ( 0 ), chr1 As Char = Chr ( 1 )
Sub New ()
Data = ""
Cnt = 0
End Sub
''' <summary>
''' Returns actual count of items within strContainer.
''' </summary>
Function Count () As Integer
Return Cnt
End Function
'''<summary>Fill StrContainer with items of listbox.</summary>
'''<param name="mode">-before mode:clear before fill, 1:adds only new, 2:adds all</param>
Sub fromListbox (ByVal lb As ListBox , ByVal mode As Integer )
Dim i , c As Integer , lb_item As String
If mode < 0 Then
Data = "" : Cnt = 0 : mode *= - 1
End If
c = lb .Items . Count
If c > 0 Then
For i = 0 To c - 1
lb_item = lb .Items . Item (i )
If (Not Contains (lb_item ) Or (Not mode = 1 )) Then Add (lb_item )
Next
End If
End Sub
''' <summary>Copy items to listbox.</summary>
''' <param name="mode">-before mode:clear before fill, 1:adds only new, 2:adds all</param>
Sub toListbox (ByRef lb As ListBox , ByVal mode As Byte )
Dim i As Integer , sc_item As String
If mode = 0 Then lb .Items . Clear ()
If Cnt > 0 Then
For i = 1 To Cnt
sc_item = Item (i )
If (Not lb .Items .Contains (sc_item )) Or (Not mode = 1 ) Then lb .Items . Add (sc_item )
Next
End If
End Sub
''' <summary>Fill StrContainer with items of other StrContainer.</summary>
''' <param name="mode">-before mode:clear before fill, 1:adds only new, 2:adds all</param>
Sub fromStrContainer (ByVal sourceSC As StrContainer , ByVal mode As Integer )
Dim i As Integer , sc_item As String
If mode < 0 Then
Data = "" : Cnt = 0 : mode *= - 1
End If
If sourceSC . Count > 0 Then
For i = 1 To sourceSC . Count
sc_item = sourceSC . Item (i )
If (Not Contains (sc_item )) Or (Not mode = 1 ) Then Add (sc_item )
Next
End If
End Sub
''' <summary>
''' Returns left strcontainer extended by new items from right strcontainer. Same as fromStrcontainer(mode 1).
''' </summary>
Public Shared Operator &(ByVal left As StrContainer , ByVal right As StrContainer ) As StrContainer
Dim i As Integer , sc_item As String , retval As New StrContainer
retval = left
If right . Count > 0 Then
For i = 1 To right . Count
sc_item = right . Item (i )
If (Not retval .Contains (sc_item )) Then retval . Add (sc_item )
Next
End If
Return retval
End Operator
''' <summary>
''' Returns left strcontainer extended by all items from right strcontainer. Same as fromStrContainer(mode 2).
''' </summary>
Public Shared Operator +(ByVal left As StrContainer , ByVal right As StrContainer ) As StrContainer
Dim i As Integer , retval As New StrContainer
retval = left
If right . Count > 0 Then
For i = 1 To right . Count
retval . Add ( right . Item (i ))
Next
End If
Return retval
End Operator
''' <summary>Returns string containing all items of StrContainer separated by 'separator'.</summary>
Function itemsToString (ByVal separator As String ) As String
Dim retval As String
If Cnt = 0 Then
Return ""
ElseIf Cnt = 1 Then
Return Item ( 1 )
End If
retval = Replace (Replace ( Left (Data , Len (Data ) - 1 ), chr0 , "" ), chr1 , separator )
Return retval
End Function
''' <summary>Adds new item.</summary>
Sub Add (ByVal item As Integer )
Data &= chr0 & Trim ( Str ( item )) & chr1
Cnt += 1
End Sub
''' <summary>Adds new item.</summary>
Sub Add (ByVal item As String )
Data &= chr0 & item & chr1
Cnt += 1
End Sub
''' <summary>Returns item at specified index or chr(0) if not found.</summary>
Function Item (ByVal index As Integer ) As String
Dim i As Integer , n As Integer = 0 , ende As Integer , retval As String = ""
If index > Cnt Or index < 1 Then Return chr0
For i = 0 To Len (Data ) - 1
If Data (i ) = chr0 Then
n += 1
If n = index Then
ende = InStr (i + 2 , Data , chr1 )
retval = Mid (Data , i + 2 , ende - i - 2 )
Return retval
End If
End If
Next
Return ""
End Function
''' <summary>Clears container.</summary>
Sub Clear ()
Cnt = 0
Data = ""
End Sub
''' <summary>Returns true if container contains item, otherwise false.</summary>
Function Contains (ByVal item As String ) As Boolean
If IndexOf ( item ) > 0 Then Return True
Return False
End Function
''' <summary>Returns true if container contains item, otherwise false.</summary>
Function Contains (ByVal item As Integer ) As Boolean
If IndexOf ( Trim ( Str ( item ))) > 0 Then Return True
Return False
End Function
''' <summary>Deletes item.</summary>
Sub Delete (ByVal item As String )
Dim part1 , part2 , needle As String , loc As Integer
needle = chr0 & item & chr1
loc = InStr (Data , needle )
If loc = 0 Then Exit Sub
part1 = Left (Data , loc - 1 )
part2 = Right (Data , Len (Data ) - loc - Len (needle ) + 1 )
Data = part1 & part2
Cnt -= 1
End Sub
''' <summary>Deletes item.</summary>
Sub Delete (ByVal item As Integer )
Call Delete ( Trim ( Str ( item )))
End Sub
''' <summary>Removes item at specified index.</summary>
Sub RemoveAt (ByVal index As Integer )
Delete ( Item (index ))
End Sub
''' <summary>Returns index of item, or 0 if not found.</summary>
Function IndexOf (ByVal item As String ) As Integer
Dim i , pos As Integer , n As Integer = 0
pos = InStr (Data , chr0 & item & chr1 )
If pos = 0 Then Return 0
For i = pos - 1 To 0 Step - 1
If Data (i ) = chr0 Then n = n + 1
Next
Return n
End Function
''' <summary>Returns index of item, or 0 if not found.</summary>
Function IndexOf (ByVal item As Integer ) As Integer
Return IndexOf ( Trim ( Str ( item )))
End Function
''' <summary>
''' Returns strcontainer filled only with items beeing in both left and right strcontainers.
''' </summary>
Public Shared Operator And(ByVal left As StrContainer , ByVal right As StrContainer ) As StrContainer
Dim i As Integer , retval As New StrContainer , item As String
For i = 1 To left . Count
item = left . Item (i )
If right .Contains ( item ) Then retval . Add ( item )
Next
Return retval
End Operator
''' <summary>
''' Returns strcontainer filled with all items beeing in left or right strcontainer.
''' </summary>
Public Shared Operator Or(ByVal left As StrContainer , ByVal right As StrContainer ) As StrContainer
Dim i As Integer , retval As New StrContainer , item As String
For i = 1 To left . Count
item = left . Item (i )
If Not retval .Contains ( item ) Then retval . Add ( item )
Next
For i = 1 To right . Count
item = right . Item (i )
If Not retval .Contains ( item ) Then retval . Add ( item )
Next
Return retval
End Operator
''' <summary>
''' Returns strcontainer filled with items beeing only in left or only in right strcontainer.
''' </summary>
Public Shared Operator Xor(ByVal left As StrContainer , ByVal right As StrContainer ) As StrContainer
Dim i As Integer , retval As New StrContainer , item As String
For i = 1 To left . Count
item = left . Item (i )
If Not right .Contains ( item ) Then retval . Add ( item )
Next
For i = 1 To right . Count
item = right . Item (i )
If Not left .Contains ( item ) Then retval . Add ( item )
Next
Return retval
End Operator
''' <summary>
''' Returns strcontainer filled with items beeing in left and not in right strcontainer.
''' </summary>
Function AndNot (ByVal left As StrContainer , ByVal right As StrContainer ) As StrContainer
Dim i As Integer , retval As New StrContainer , item As String
For i = 1 To left . Count
item = left . Item (i )
If Not right .Contains ( item ) Then retval . Add ( item )
Next
Return retval
End Function
End Class
Briefly, it has same item working mechanism as e.g. ListBox... and also it 'builtin-cooperates' with listbox. Also added field logical-like operations And, Or, Xor, AndNot, which enables you to compare, or merge etc your string/integer containers...
''' <summary>Container of integer or string values. Do not store chr(0) and chr(1) in items.</summary>
Public Class StrContainer
Dim Data As String
Dim Cnt As Integer
Dim chr0 As Char = Chr ( 0 ), chr1 As Char = Chr ( 1 )
Sub New ()
Data = ""
Cnt = 0
End Sub
''' <summary>
''' Returns actual count of items within strContainer.
''' </summary>
Function Count () As Integer
Return Cnt
End Function
'''<summary>Fill StrContainer with items of listbox.</summary>
'''<param name="mode">-before mode:clear before fill, 1:adds only new, 2:adds all</param>
Sub fromListbox (ByVal lb As ListBox , ByVal mode As Integer )
Dim i , c As Integer , lb_item As String
If mode < 0 Then
Data = "" : Cnt = 0 : mode *= - 1
End If
c = lb .Items . Count
If c > 0 Then
For i = 0 To c - 1
lb_item = lb .Items . Item (i )
If (Not Contains (lb_item ) Or (Not mode = 1 )) Then Add (lb_item )
Next
End If
End Sub
''' <summary>Copy items to listbox.</summary>
''' <param name="mode">-before mode:clear before fill, 1:adds only new, 2:adds all</param>
Sub toListbox (ByRef lb As ListBox , ByVal mode As Byte )
Dim i As Integer , sc_item As String
If mode = 0 Then lb .Items . Clear ()
If Cnt > 0 Then
For i = 1 To Cnt
sc_item = Item (i )
If (Not lb .Items .Contains (sc_item )) Or (Not mode = 1 ) Then lb .Items . Add (sc_item )
Next
End If
End Sub
''' <summary>Fill StrContainer with items of other StrContainer.</summary>
''' <param name="mode">-before mode:clear before fill, 1:adds only new, 2:adds all</param>
Sub fromStrContainer (ByVal sourceSC As StrContainer , ByVal mode As Integer )
Dim i As Integer , sc_item As String
If mode < 0 Then
Data = "" : Cnt = 0 : mode *= - 1
End If
If sourceSC . Count > 0 Then
For i = 1 To sourceSC . Count
sc_item = sourceSC . Item (i )
If (Not Contains (sc_item )) Or (Not mode = 1 ) Then Add (sc_item )
Next
End If
End Sub
''' <summary>
''' Returns left strcontainer extended by new items from right strcontainer. Same as fromStrcontainer(mode 1).
''' </summary>
Public Shared Operator &(ByVal left As StrContainer , ByVal right As StrContainer ) As StrContainer
Dim i As Integer , sc_item As String , retval As New StrContainer
retval = left
If right . Count > 0 Then
For i = 1 To right . Count
sc_item = right . Item (i )
If (Not retval .Contains (sc_item )) Then retval . Add (sc_item )
Next
End If
Return retval
End Operator
''' <summary>
''' Returns left strcontainer extended by all items from right strcontainer. Same as fromStrContainer(mode 2).
''' </summary>
Public Shared Operator +(ByVal left As StrContainer , ByVal right As StrContainer ) As StrContainer
Dim i As Integer , retval As New StrContainer
retval = left
If right . Count > 0 Then
For i = 1 To right . Count
retval . Add ( right . Item (i ))
Next
End If
Return retval
End Operator
''' <summary>Returns string containing all items of StrContainer separated by 'separator'.</summary>
Function itemsToString (ByVal separator As String ) As String
Dim retval As String
If Cnt = 0 Then
Return ""
ElseIf Cnt = 1 Then
Return Item ( 1 )
End If
retval = Replace (Replace ( Left (Data , Len (Data ) - 1 ), chr0 , "" ), chr1 , separator )
Return retval
End Function
''' <summary>Adds new item.</summary>
Sub Add (ByVal item As Integer )
Data &= chr0 & Trim ( Str ( item )) & chr1
Cnt += 1
End Sub
''' <summary>Adds new item.</summary>
Sub Add (ByVal item As String )
Data &= chr0 & item & chr1
Cnt += 1
End Sub
''' <summary>Returns item at specified index or chr(0) if not found.</summary>
Function Item (ByVal index As Integer ) As String
Dim i As Integer , n As Integer = 0 , ende As Integer , retval As String = ""
If index > Cnt Or index < 1 Then Return chr0
For i = 0 To Len (Data ) - 1
If Data (i ) = chr0 Then
n += 1
If n = index Then
ende = InStr (i + 2 , Data , chr1 )
retval = Mid (Data , i + 2 , ende - i - 2 )
Return retval
End If
End If
Next
Return ""
End Function
''' <summary>Clears container.</summary>
Sub Clear ()
Cnt = 0
Data = ""
End Sub
''' <summary>Returns true if container contains item, otherwise false.</summary>
Function Contains (ByVal item As String ) As Boolean
If IndexOf ( item ) > 0 Then Return True
Return False
End Function
''' <summary>Returns true if container contains item, otherwise false.</summary>
Function Contains (ByVal item As Integer ) As Boolean
If IndexOf ( Trim ( Str ( item ))) > 0 Then Return True
Return False
End Function
''' <summary>Deletes item.</summary>
Sub Delete (ByVal item As String )
Dim part1 , part2 , needle As String , loc As Integer
needle = chr0 & item & chr1
loc = InStr (Data , needle )
If loc = 0 Then Exit Sub
part1 = Left (Data , loc - 1 )
part2 = Right (Data , Len (Data ) - loc - Len (needle ) + 1 )
Data = part1 & part2
Cnt -= 1
End Sub
''' <summary>Deletes item.</summary>
Sub Delete (ByVal item As Integer )
Call Delete ( Trim ( Str ( item )))
End Sub
''' <summary>Removes item at specified index.</summary>
Sub RemoveAt (ByVal index As Integer )
Delete ( Item (index ))
End Sub
''' <summary>Returns index of item, or 0 if not found.</summary>
Function IndexOf (ByVal item As String ) As Integer
Dim i , pos As Integer , n As Integer = 0
pos = InStr (Data , chr0 & item & chr1 )
If pos = 0 Then Return 0
For i = pos - 1 To 0 Step - 1
If Data (i ) = chr0 Then n = n + 1
Next
Return n
End Function
''' <summary>Returns index of item, or 0 if not found.</summary>
Function IndexOf (ByVal item As Integer ) As Integer
Return IndexOf ( Trim ( Str ( item )))
End Function
''' <summary>
''' Returns strcontainer filled only with items beeing in both left and right strcontainers.
''' </summary>
Public Shared Operator And(ByVal left As StrContainer , ByVal right As StrContainer ) As StrContainer
Dim i As Integer , retval As New StrContainer , item As String
For i = 1 To left . Count
item = left . Item (i )
If right .Contains ( item ) Then retval . Add ( item )
Next
Return retval
End Operator
''' <summary>
''' Returns strcontainer filled with all items beeing in left or right strcontainer.
''' </summary>
Public Shared Operator Or(ByVal left As StrContainer , ByVal right As StrContainer ) As StrContainer
Dim i As Integer , retval As New StrContainer , item As String
For i = 1 To left . Count
item = left . Item (i )
If Not retval .Contains ( item ) Then retval . Add ( item )
Next
For i = 1 To right . Count
item = right . Item (i )
If Not retval .Contains ( item ) Then retval . Add ( item )
Next
Return retval
End Operator
''' <summary>
''' Returns strcontainer filled with items beeing only in left or only in right strcontainer.
''' </summary>
Public Shared Operator Xor(ByVal left As StrContainer , ByVal right As StrContainer ) As StrContainer
Dim i As Integer , retval As New StrContainer , item As String
For i = 1 To left . Count
item = left . Item (i )
If Not right .Contains ( item ) Then retval . Add ( item )
Next
For i = 1 To right . Count
item = right . Item (i )
If Not left .Contains ( item ) Then retval . Add ( item )
Next
Return retval
End Operator
''' <summary>
''' Returns strcontainer filled with items beeing in left and not in right strcontainer.
''' </summary>
Function AndNot (ByVal left As StrContainer , ByVal right As StrContainer ) As StrContainer
Dim i As Integer , retval As New StrContainer , item As String
For i = 1 To left . Count
item = left . Item (i )
If Not right .Contains ( item ) Then retval . Add ( item )
Next
Return retval
End Function
End Class
Related articles
Related discussion
-
Sharepoint : GroupBy results according to custom property
by sampadasanjay (0 replies)
-
i have struck with my project in vb.net
by jetski (4 replies)
-
Error in VB code
by glib162002 (0 replies)
-
Very slow inserts using SqlCommand.ExecuteNonQuery()
by porchelvi (1 replies)
-
Datagridview Setting datasource property of datagridviewcomboboxcell at run time
by sairfan1 (2 replies)
Related jobs
-
Microsoft .Net Architect
in AMSTERDAM (€50K-€90K per annum)
Events coming up
-
Dec
6
Developing AJAX Web Applications with Castle Monorail
London, United Kingdom
Monorail is the model-view-controller engine of the Castle Project, bringing many of the best ideas of Ruby on Rails to the .NET world. In this talk, David De Florinier and Gojko Adzic show how Monorail makes it easy to develop .NET based AJAX applications, and how to use the Castle Project to build Web 2.0 applications effectively. Come to this session if you are a .NET web developer. Everyone is welcome!
This thread is for discussions of String container (mutant class, really).