Data Structures For VB
Data Structures For VB
VB programmers
Francesco Balena
Agenda
2
The System.Object class
4
String class
5
The Char class
6
The StringBuilder class
7
The StringBuilder class
Imports System.Text
8
Arrays
9
The Array class
Instance read-only properties
Rank returns the number of dimensions
Length returns the total number of elements
Instance methods
GetLength(n) number of elements along a given dimension
GetLowerBound(n), GetUppperBound(n)
like LBound,UBound, except N is zero based, not 1-based
Clone method creates a copy
does a shallow copy (as opposed to a deep copy)
The Array.Reverse method reverses the order
Reverse(arr) or Reverse(arr, start, length) variations
10
Sorting arrays
11
IComparable interface
Dim p As Person
For Each p In Persons
Debug.WriteLine(p.ReverseName)
Next
12
IComparable interface
Class Person
Implements IComparable
Public FirstName, LastName As String
13
IComparer interface
14
IComparer interface
Class Person
' ...
Class CompareByName
Implements IComparer
Function ComparePers(ByVal o1 As Object, ByVal o2 As Object) _
As Integer Implements IComparer.Compare
IComparer.Compare
Dim p1 As Person = CType(o1, Person)
Dim p2 As Person = CType(o2, Person)
Return StrComp(p1.CompleteName, p2.CompleteName, _
CompareMethod.Text)
End Function
End Class
Class CompareByReverseName
Implements IComparer
' note how you can avoid name conflits
Function [Compare](ByVal o1 As Object, ByVal o2 As Object) _
As Integer Implements IComparer.Compare
IComparer.Compare
' note the on-the-fly casts
Return StrComp(CType
CType(o1,
CType(o1, Person).ReverseName,
Person). _
CType(o
CType(o2
(o2, Person).ReverseName,
Person). CompareMethod.Text)
End Function
End Class
End Class
15
Deleting and inserting elements
16
Searching values
17
Searching values
18
Collections and Dictionaries
19
The System.Collections namespaces
20
Main collection interfaces
ICollection interface: an unordered group of items
inherits from IEnumerable (For Each support)
Count read-only property
CopyTo copies data into a destination array
IList interface: items that can be individually indexed
inherits from ICollecton (For Each support, Count, CopyTo)
Item(index) property sets or returns an element
Add(obj), Insert(obj, index) methods add and element
Remove(obj), RemoveAt(index), Clear remove elements
Contains(obj), IndexOf(obj) methods search an element
IDictionary interface: items that can be searched by a key
inherits from ICollecton (For Each support, Count, CopyTo)
Item(obj) property sets or returns an element
Add(key, obj) method add an element
Remove(key), Clear methods remove one/all elements
Keys, Values properties return the ICollection of keys/values
Contains(key) method tests whether a key is there
21
The BitArray class
22
The BitArray class
' An array of 1000 values set to True
Dim ba As New BitArray(1000,
BitArray True)
' AND all the bits with the complement of bits in ba2.
ba1.And
And(ba2.Not
And Not)
Not
23
The Stack class
24
The Stack class
25
The Queue class
26
The Queue class
' Extract the last element, check that the queue is now empty
Debug.WriteLine(qu.Dequeue
Dequeue)
Dequeue ' => 30
Debug.WriteLine(qu.Count
Count)
Count ' => 0
27
The ArrayList class
The ArrayList class can be considered the combination of
the Array and Collection classes
as an array: dim, reference by index, sort, reverse, search
an a collection: append and insert elements, remove them
Specify an initial capacity in the constructor (default is 16)
Dim al As New ArrayList(1000)
can also change by assigning to the Capacity property
al.Capacity = 2000
you can't control the growth factor (it is always 2)
the TrimToSize method shrinks the ArrayList
ArrayList implements IList
Add, Insert, Remove, RemoveAt, Clear, etc.
The ArrayList.Repeat method creates an ArrayList and
initializes it elements
Dim al As ArrayList = ArrayList.Repeat("", 100)
28
The ArrayList class
' Remove removes only one element - Use a loop to remove all
Do
Try
al.Remove
Remove("element
Remove to remove")
Catch
Exit Do
End Try
Loop
29
The ArrayList class
30
The ArrayList class
31
The StringCollection class
32
The Hashtable class
33
The Hashtable class
ht.Add
Add("Joe",
Add 12000) ' Syntax is .Add(key, value)
ht.Add
Add("Ann",
Add 13000)
' Referencing a new key creates an element
ht.Item
Item("Robert")
Item = 15000
34
The Hashtable class
35
The Hashtable class
Imports System.Collections
Dim ht As Hashtable = _
Specialized.CollectionsUtil
Specialized.CollectionsUtil.
CollectionsUtil.CreateCaseInsensitiveHashtable()
CreateCaseInsensitiveHashtable
36
The SortedList class
37
The SortedList class
Class ReverseStringComparer
Implements IComparer
Function CompareValues(x As Object, y As Object) _
As Integer Implements IComparer.Compare
' Just change the sign of the Compare method
Return -String.Compare(x.ToString, y.ToString)
End Function
End Class
38
The SortedList class
Other methods
ContainsKey(obj) checks whether a key exists
ContainsValue(obj) checks whether a value exists
GetKey(index) returns a key at given position
GetByIndex(index) returns a value at given position
IndexOfKey(obj) returns the index of a given key
IndexOfValue(obj) returns the index of a given value
SetByIndex(index, obj) assigns a new value to an item
similar to Item, but works with the index instead of key
39
String collections and dictionaries
40
Custom collections
41
Inheriting new collections
The easiest way to create a new collection class is inheriting
from an abstract class provided by the runtime
they implement much of the functionality, you just add the
missing pieces
ReadOnlyCollectionBase class: read-only collections
can refer to inner ArrayList through the InnerList property
only need to create the collection in the constructor and
implement the Item property (no need for Add and Remove)
can be read-only or read-write
CollectionBase class: strongly-typed collections
can refer to inner ArrayList through the InnerList property
must implement only Add and Item members
may add a Create method that works as a constructor
DictionaryBase class: strongly-typed dictionary
can refer to inner dictionary through the Dictionary property
must implement only Add and Item members
42
ReadOnlyCollectionBase class
Class PowersOfTwoCollection
Inherits System.Collections.ReadOnlyCollectionBase
System.Collections.ReadOnlyCollectionBase
43
CollectionBase class
' A collection object that can only store Square objects
Class SquareCollection
Inherits System.Collections.CollectionBase
System.Collections.CollectionBase
Sub Add(
Add(ByVal
ByVal Value As Square) ' strongly-typed Add
InnerList.Add(Value)
InnerList
End Sub
44
DictionaryBase class
Class SquareDictionary
Inherits System.Collections.DictionaryBase
System.Collections.DictionaryBase
Sub Add(
Add(ByVal
ByVal Key As String, ByVal Value As Square)
Dictionary.Add(Key,
Dictionary Value)
End Sub
45
IEnumerable and IEnumerator
interfaces
Together they add support for enumeration (For Each)
IEnumerable.GetEnumerator returns an object that
supports IEnumerator
IEnumerator exposes three methods
MoveNext() As Boolean, returns True if there are more items
Current, returns the current value
Reset, resets the internal pointer
46
IEnumerable and IEnumerator
interfaces
Class PowersOfTwo
Implements IEnumerable
47
IEnumerable and IEnumerator
interfaces
Class PowersOfTwoEnumerator
Implements IEnumerator
Dim MaxValue, CurValue As Integer
48
Regular Expressions
49
Introduction to regular expressions
50
The Regex hierarchy
RegEx
Matches property
MatchCollection
Match
Groups property
Group
Captures property
CaptureCollection
Capture
51
The Regex hierarchy
52
Enumerating matches
53
Replacing substrings
54
Character escapes
55
Character classes
56
Atomic zero-width assertions
57
Quantifiers
58
Grouping constructs
59
Alternating constructs
60
Positional constructs
61
RegEx options
62
Search variations
Dim s As String
For Each s In re.Split
Split(source)
Split
' Note that the 3rd element is a null string.
Debug.Write(s & "-") ' => 123-456--789
Next
63
Replace substrings
64
Replace with callback
65
Questions ?
66