murach’s visual basic 2008, modified, c8© 2008, mike murach & associates, inc. slide 1
TRANSCRIPT
No Slide Title
Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 1
1How to Declare and Instantiate an ArrayMurachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 2Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 3
3What if you do not know the size needed?Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 4Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 5
5Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 6
6Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 7
7Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 8
8Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 9
9How to reference the elements of an array?Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 10Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 11
11Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 12
12Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 13
13Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 14
14Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 15
15Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 16
16Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 17
17To Enhance Your UnderstandingRead pp. 223-229Do Exercise 8-1 #1-3Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 18Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 19
19Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 20
20Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 21
21To Enhance Your UnderstandingRead pp. 223-231Do Exercise 8-1 #1-7Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 22Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 23
Syntax for Rectangular Arrays23Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 24
24Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 25
25Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 26
26Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 27
27Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 28
28To Enhance Your UnderstandingRead pp. 223-235Do Exercise 8-2Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 29Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 30
30Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 31
31Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 32
32Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 33
33Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 34
34Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 35
35Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 36
36Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 37
37Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 38
38Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 39
39Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 40
40Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 41
41Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 42
42Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 43
43Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 44
44Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 45
45Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 46
46Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 47
47Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 48
48Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 49
49Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 50
50Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 51
51Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 52
52Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 53
53Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 54
54Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 55
55Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 56
56To Enhance your Learning:Read pp. 252-253Do Exercise 8-1, # 9 & 10Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 57The SortedList() ClassUses a key to access each elementInefficient for sequential accessEfficient for insertinf elements into the listSorted key order maintained automaticallyMurachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 58KeyValuePair StructureTwo PropertiesKeyValue Value can storeValue type orReference type
Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 59Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 60
60Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 61
61Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 62
62Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 63
63Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 64
64Lab AssignmentCreate a project that will test the code on p. 255Create a form with an appropriate interface to work with the code.Allow the user to enter key value pairs on the form.Allow the user to specify for whom they wish to search.Allow the user to request a display of the data.Test the code only. Do not worry about validating data, etc.Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 65Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 66
66Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 67
67Lab Assignment, QueueCreate a project that will test the code on p. 257Create a form with an appropriate interface to work with the code.Allow the user to enter names to put in the queue.Allow the user to specify for whom they wish to search (code not supplied).Allow the user to request a display of the names in the queue.Test the code only. Do not worry about validating data, etc.
Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 68Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 69
69Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 70
70ArrayListIs an Untyped collection (legacy)Works mostly like a List()Is not on testMurachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 71Lab Assignment, StacksCreate a project that will test the code on p. 257Create a form with an appropriate interface to work with the code.Allow the user to enter names to put in the stack.Allow the user to specify for whom they wish to search (code not supplied).Allow the user to request a display of the names in the stack.Allow the user to peek.Test the code only. Do not worry about validating data, etc.
Murachs Visual Basic 2008, modified, C8 2008, Mike Murach & Associates, Inc.Slide 72Chapter 8
How to work with arrays and collections
Declaring and initializing a one-dimensional array
Dim arrayName(upperbound) As type
Index goes from 0 to upperbound.
Array length or size is upperbound + 1.ExampleDim numbers(3) As Integer
Declaring but not initializing a one-dimensional arrayDim arrayName() As type
Example
Dim totals() As Decimal
Initializing an array that has only been declaredarrayName = New type(upperbound) {}
Example
Dim totals() As Decimal
totals = New Decimal(49) {}
Getting the upper bound from a variableDim maxTotals As Integer = CInt(txtUpperBound.Text)
Dim totals(maxTotals) As DecimalUsing the New keyword to initialize an array
Dim maxTotals As Integer = CInt(txtUpperBound.Text)
Dim totals() As Decimaltotals = New Decimal(maxTotals) {}
How to document the lower bound when you declare an array
Dim titles(0 To 49) As String ' provides for 50 titles
Default values for array elements
Data typeDefault value
All numeric types0 (zero)
CharNull (binary 0)
BooleanFalse
Date01/01/0001 00:00:00
All reference typesNull reference
The syntax for referring to an element of an array
arrayName(index)Code that assigns values to an array of Decimal types
Dim totals(3) As Decimal ' this provides for 4 elements
totals(0) = 14.95D
totals(1) = 12.95D
totals(2) = 11.95D
totals(3) = 9.95D
'totals(4) = 8.95D
' this would throw an IndexOutOfRangeException
Code that assigns objects to an array of strings
Dim names(2) As String
names(0) = "Ted Lewis"
names(1) = "Sue Jones"
names(2) = "Ray Thomas"
The syntax for creating an array and assigning values in one statement
Dim arrayName() As type = [New type(upperbound)] {value1[, value2]
[, value3]...}
Examples
Dim totals() As Decimal = {14.95D, 12.95D, 11.95D, 9.95D}
Dim names() as String =
{"Ted Lewis", "Sue Jones", "Ray Thomas"}
Dim totals() As Decimal =
New Decimal(3) {14.95D, 12.95D, 11.95D, 9.95D}
The syntax for using the Length property of an array
arrayName.Length
A For loop that puts the numbers 0 through 9 into an array Fill the array.Dim numbers(9) As Integer
For i As Integer = 0 To numbers.Length - 1
numbers(i) = i
Next
Code that displays the numbers array in a message box
Dim numbersString As String = ""
For i As Integer = 0 To numbers.Length - 1
numbersString &= numbers(i).ToString & " "
Next
MessageBox.Show(numbersString, "Numbers Test")
The message box thats displayed
Code that uses a For loop to compute the average of a totals array
Dim totals() As Decimal = {14.95D, 12.95D, 11.95D, 9.95D}
Dim sum As Decimal = 0
For i As Integer = 0 To totals.Length - 1
sum += totals(i)
Next
Dim average As Decimal = sum / totals.Length
Code that displays the totals array in a message box
Dim totalsString As String = ""
For i As Integer = 0 To totals.Length - 1
totalsString &= total.ToString & vbCrLf
Next
MessageBox.Show("The totals are:" _
& vbCrLf & totalsString & vbCrLf & _
"Sum: " & sum & vbCrLf & "Average: " & average, _
"Totals Test")
The message box thats displayed
The syntax of a For Each loop
For Each elementName [As type] In arrayName statements
Next
Code that displays the numbers array in a message box
Dim numbersString As String = ""
For Each number As Integer In numbers
numbersString &= number.ToString & " "Next
MessageBox.Show(numbersString, "Numbers Test")
The message box thats displayed
Code that computes the average of the totals array
Dim sum As Decimal = 0
For Each total As Decimal In totals
sum += total
Next
Dim average As Decimal = sum / totals.Length
Code that displays the totals array in a message box
Dim totalsString As String = ""
For Each total In totals
totalsString &= total.ToString & vbCrLf
Next
MessageBox.Show("The totals are:" _
& vbCrLf & totalsString & vbCrLf & _
"Sum: " & sum & vbCrLf & "Average: " & average, _
"Totals Test")
The message box thats displayed
Declaring a rectangular array
Dim arrayName(firstupperbound, secondupperbound) As type
Example: Create a 3x2 array
Dim numbers(2,1) As Integer
Referring to an element of a rectangular array
arrayName(rowindex, columnindex)Code that assigns values to the numbers array
numbers(0,0) = 1
numbers(0,1) = 2
numbers(1,0) = 3
numbers(1,1) = 4
numbers(2,0) = 5
numbers(2,1) = 6
Code that creates a 3x2 array and assigns values with one statement
Dim numbers(,) As Integer = { {1,2}, {3,4}, {5,6} }
Code that creates and assigns values to a 3x2 array of strings
Dim products(,) As String = _
{{"VB08", "Murach's Visual Basic 2008"}, _
{"JSE6", "Murach's Java SE 6"}, _
{"A3VB", "Murach's ASP.NET 3.5 with VB 2008"}}
The syntax for using the GetLength method of a rectangular array
arrayName.GetLength(dimensionindex)
Code that works with the numbers array
Dim numbers(,) As Integer = { {1,2}, {3,4}, {5,6} }
Dim numberOfRows As Integer = numbers.GetLength(0)
Dim numberOfColumns As Integer = numbers.GetLength(1)
Dim sumOfFirstRow As Integer = numbers(0,0) _
+ numbers(0,1)
Displays the numbers array in a message box
Dim numbersString As String = ""
For i As Integer = 0 To numbers.GetLength(0) - 1 For j As Integer = 0 To numbers.GetLength(1) - 1 numbersString &= numbers(i, j) & " "
Next
numbersString &= vbCrLf
Next
MessageBox.Show(numbersString, "Numbers Test")
The message box thats displayed
Displays the products array in a message box
Dim productsString As String = ""
For i As Integer = 0 To products.GetLength(0) 1 For j As Integer = 0 To products.GetLength(1) - 1
productsString &= products(i, j) & vbTab
Next j
productsString &= vbCrLf
Next iMessageBox.Show(productsString, "Products Test")
The message box thats displayed
The syntax for declaring a jagged array
Dim arrayName(numberofrows)() As typeCode that declares a jagged array with three rows
Dim numbers (2)() As Integer numbers(0) = New Integer(2) {} numbers(1) = New Integer(3) {} numbers(2) = New Integer(1) {} The syntax for referring to an element of a jagged array
arrayName(rowindex)(columnindex)Statements that assign values to the numbers array
numbers(0)(0) = 1numbers(1)(0) = 4numbers(2)(0) = 8
numbers(0)(1) = 2numbers(1)(1) = 5numbers(2)(1) = 9
numbers(0)(2) = 3numbers(1)(2) = 6
numbers(1)(3) = 7
Code that creates the numbers array with one statement
Dim numbers()() As Integer = {New Integer() {1, 2, 3}, _
New Integer() {4, 5, 6, 7}, _
New Integer() {8, 9}}Code that creates a jagged array of stringsDim titles()() As String = _
{New String(2) {"War and Peace", "Wuthering Heights", _
"1984"}, _
New String(3) {"Casablanca", "Wizard of Oz", _
"Star Wars", "Birdy"}, _
New String(1) {"Blue Suede Shoes", "Yellow Submarine"}}
Code that displays the numbers array in a message box
Dim numbersString As String = ""
For i As Integer = 0 To numbers.GetLength(0) - 1
For j As Integer = 0 To numbers(i).Length - 1
numbersString &= numbers(i)(j) & " "
Next numbersString &= vbCrLf
NextMessageBox.Show(numbersString, "Jagged Numbers Test")
The message box thats displayed
Code that displays the titles array in a message box
Dim numbersString As String = ""
For i As Integer = 0 To titles.GetLength(0) - 1
For j As Integer = 0 To titles(i).Length - 1
numbersString &= titles(i)(j) & " "
Next j
numbersString &= vbCrLf
Next i
MessageBox.Show(numbersString, "Jagged Titles Test")
The message box thats displayed
Common properties and methods of the Array class
PropertyDescription
LengthGets the number of elements in all of the dimensions of an array.
Instance methodDescription
GetLength(dimension)Gets the number of elements in the specified dimension of an array.
GetUpperBound(dimension)Gets the index of the last element in the specified dimension of an array.
Common properties and methods of the Array class (continued)Static methodDescription
Copy(array1, array2, length)Copies some or all of the values in one array to another array.
BinarySearch(array, value)Searches a one-dimensional array thats in ascending order for an element with a specified value and returns the index for that element.
Sort(array)Sorts the elements in a one-dimensional array into ascending order.
Code that uses the GetLength and GetUpperBound methods
Dim numbers() As Integer = {1, 2, 3, 4}Dim length As Integer = numbers.GetLength(0)Dim upperBound As Integer = numbers.GetUpperBound(0)Code that uses the Sort method
Dim lastNames() As String = {"Boehm", "Taylor", _
"Murach", "Vasquez"}
Array.Sort(lastNames)
Dim message As String = ""
For Each lastName As String In lastNames
message &= lastName & vbCrLf
Next
MessageBox.Show(message, "Sorted Last Names")
The message box thats displayed
Code that uses the BinarySearch method
Dim employees() As String = {"Adams", "Finkle", _
"Lewis", "Potter"}
Dim salesAmounts() As Decimal = {3275.68D, 4298.55D, _
5289.57D, 1933.98D}
Dim index As Integer = Array.BinarySearch(employees, _
"Finkle")
Dim salesAmount As Decimal = salesAmounts(index)Code that creates a reference to another array
Dim inches1() As Double = {1, 2, 3}
Dim inches2() As Double = inches1
How to copy elements of one array to another array
The syntax for copying elements of an array
Array.Copy(fromarray, toarray, length)Another way to copy elements from one array to another
Array.Copy(fromarray, fromindex, toarray, toindex, length)Code that copies all the elements of an array
Dim inches() As Double = {1, 2, 3, 4}
Dim centimeters(3) As Double
Array.Copy(inches, centimeters, inches.Length)For i As Integer = 0 To centimeters.Length - 1
centimeters(i) *= 2.54NextCode that copies some of the elements of an array
Dim names() As String = {"Boehm", "Murach", "Vasquez"}
Dim lastTwoNames(1) As StringArray.Copy(names, 1, lastTwoNames, 0, 2)
How to return an array from a procedure
A Function procedure that returns an array
Private Function RateArray( _
ByVal elementCount As Integer) As Decimal()
Dim rates(elementCount - 1) As Decimal
For i As Integer = 0 To rates.Length - 1
rates(i) = (i + 1) / 100D
Next
Return rates
End Function
A statement that calls the procedure
Dim rates() As Decimal = Me.RateArray(4)
How to code a procedure that accepts an array argument
A Sub procedure that converts an array of inches to an array of centimeters
Private Sub ConvertToCentimeters( _
ByRef measurements() As Double)
For i As Integer = 0 To measurements.Length - 1
measurements(i) *= 2.54
Next
End Sub
Statements that declare an array and call the procedure
Dim measurements() As Double = {1, 2, 3}
Me.ConvertToCentimeters(measurements)
Arrays and Collections both can:
Store multiple elementsBe value types or reference types.
Differences:Class
An array inherits the Array class. Collections are other classes.
Different methodsSize
Arrays - fixed Collections -- variable
Commonly used collection classes
.NET 2.0 to 3.5.NET 1.xDescription
List()ArrayListUses an index to access each element. Efficient for accessing elements sequentially; inefficient for inserting elements into a list.
SortedList()SortedListUses a key to access a value, which can be any type of object. Efficient for inserting elements into a list; can be inefficient for accessing elements sequentially.
Queue()QueueUses special methods to add and remove elements.
Stack()StackUses special methods to add and remove elements.
Untyped collections: An example
Dim numbers As New ArrayListnumbers.Add(3)numbers.Add(70)
numbers.Add("Test")Dim sum As Integer = 0
Dim number As Integer
For i As Integer = 0 To numbers.Count - 1
number = CInt(numbers(i))
sum += number
Next
Typed collections: An example
Dim numbers As New List(Of Integer)numbers.Add(3)
numbers.Add(70)
'numbers("Test")Dim sum As Integer = 0
Dim number As IntegerFor i As Integer = 0 To numbers.Count - 1
number = numbers(i) sum += number
Next
A statement that creates a list of String elements
Dim titles As New List(Of String)
A statement that creates a list of Decimal elements
Dim prices As New List(Of Decimal)
A statement that creates a list of strings with a capacity of 3
Dim lastNames As New List(Of String)(3)
Common properties and methods of the List() class
PropertyDescription
Item(index)Gets or sets the element at the specified index. The index for the first item in a list is 0. Since Item is the default property, its name can be omitted.
CapacityGets or sets the number of elements the list can hold.
CountGets the number of elements in the list.
MethodDescription
Add(object)Adds an element to the end of a list and returns the elements index.
Clear()Removes all elements from the list and sets its Count property to zero.
Common properties and methods of the List() class (continued)
MethodDescription
Contains(object)Returns a Boolean value that indicates if the list contains the specified object.
Insert(index, object)Inserts an element into a list at the specified index.
Remove(object)Removes the first occurrence of the specified object from the list.
RemoveAt(index)Removes the element at the specified index of a list.
BinarySearch(object)Searches a list for a specified object and returns the index for that object.
Sort()Sorts the elements in a list into ascending order.
Code that causes the size of the list of names to be increased
Dim lastNames As New List(Of String)(3) ' capacity is 3 elements
lastNames.Add("Boehm")
lastNames.Add("Prince")
lastNames.Add("Murach")
lastNames.Add("Taylor") ' capacity is doubled to 6 elementslastNames.Add("Vasquez")
lastNames.Add("Steelman")
lastNames.Add("Slivkoff") ' capacity is doubled to 12 elements
The syntax for retrieving a value from a list
listName[.Item](index)Code that creates a list that holds Decimal values
Dim newSalesTotals() As Decimal = {3275.68D, 4398.55D, _
5289.75D, 1933.98D}
Dim salesTotals As New List(Of Decimal)
For Each d As Decimal In newSalesTotals
salesTotals.Add(d)
Next
Code that retrieves the first value from the list
Dim sales1 As Decimal = salesTotals(0) ' sales1 = 3275.68Code that inserts and removes an element from the list
salesTotals.Insert(0, 2745.73D) sales1 = salesTotals(0) Dim sales2 As Decimal = salesTotals(1)
salesTotals.RemoveAt(1) sales2 = salesTotals(1)
Code that displays the list in a message box
Dim salesTotalsString As String = ""
For Each d As Decimal In salesTotals
salesTotalsString &= d.ToString & vbCrLf
Next
MessageBox.Show(salesTotalsString, "Sales Totals")
The message box thats displayed
Code that checks for an element in the list and removes it if it exists
Dim x As Decimal = 2745.73D
If salesTotals.Contains(x) Then
salesTotals.Remove(x)
End IfCode that sorts and searches the list
salesTotals.Sort()
Dim sales2Index As Integer = _
salesTotals.BinarySearch(sales2)' sales2Index = 1Common properties of the SortedList() class
PropertyDescription
Item(key)Gets or sets the value of the element with the specified key. Since Item is the default property, its name can be omitted.
KeysGets a collection that contains the keys in the list.ValuesGets a collection that contains the values in the list.
CapacityGets or sets the number of elements the list can hold.
CountGives the number of elements in the list.
Common methods of the SortedList() class
MethodDescription
Add(key, value)Adds an element with the specified key and value to the sorted list.
Clear()Removes all elements from the sorted list.
ContainsKey(key)Returns a Boolean value that indicates whether or not the sorted list contains the specified key.
ContainsValue(value)Returns a Boolean value that indicates whether or not the sorted list contains the specified value.
Common methods of the SortedList() class (cont.)MethodDescription
Remove(key)Removes the element with the specified key from the sorted list.
RemoveAt(index)Removes the element at the specified index from the sorted list.
Properties of the KeyValuePair structure
PropertyDescription
KeyThe key for the SortedList item.
ValueThe value associated with the key.
Code that creates and loads a sorted list
Dim salesList As New SortedList(Of String, Decimal)
salesList.Add("Adams", 3274.68D)
salesList.Add("Finkle", 4398.55D)
salesList.Add("Lewis", 5289.75D)
salesList.Add("Potter", 1933.97D)
Code that looks up a value in the sorted list based on a key
Dim employeeKey As String = "Lewis"
Dim salesTotal As Decimal = salesList(employeeKey)
Code that converts the sorted list to a tab-delimited string
Dim salesTableString As String = ""
For Each employeeSalesEntry _
As KeyValuePair(Of String, Decimal) In salesList
salesTableString &= employeeSalesEntry.Key _
& vbTab & _
employeeSalesEntry.Value & vbCrLf
Next
MessageBox.Show(salesTableString, "Sorted List Totals")
Properties and methods of the Queue() class
PropertyDescription
CountGets the number of items in the queue.
MethodDescription
Enqueue(object)Adds the specified object to the end of the queue.
Dequeue()Gets the object at the front of the queue and removes it from the queue.
Clear()Removes all items from the queue.
Peek()Retrieves the next item in the queue without deleting it.
Code that uses a queue
Dim nameQueue As New Queue(Of String)
nameQueue.Enqueue("Boehm")
nameQueue.Enqueue("Murach")
nameQueue.Enqueue("Vasquez")
Dim nameQueueString As String = ""
Do While nameQueue.Count > 0
nameQueueString &= nameQueue.Dequeue & vbCrLf
Loop
MessageBox.Show(nameQueueString, "Queue")The message thats displayed
Properties and methods of the Stack() class
PropertyDescription
CountGets the number of items in the stack.
MethodDescription
Push(object)Adds the specified object to the top of the stack.
Pop()Gets the object at the top of the stack and removes it from the stack.
Clear()Removes all items from the stack.
Peek()Retrieves the next item in the stack without deleting it.
Code that uses a stack
Dim nameStack As New Stack(Of String)
nameStack.Push("Boehm")
nameStack.Push("Murach")
nameStack.Push("Vasquez")
Dim nameStackString As String = ""
Do While nameStack.Count > 0
nameStackString &= nameStack.Pop & vbCrLf
Loop
MessageBox.Show(nameStackString, "Stack")The message thats displayed