
- •Using Your Sybex Electronic Book
- •Acknowledgments
- •Contents at a Glance
- •Introduction
- •Who Should Read This Book?
- •How About the Advanced Topics?
- •The Structure of the Book
- •How to Reach the Author
- •The Integrated Development Environment
- •The Start Page
- •Project Types
- •Your First VB Application
- •Making the Application More Robust
- •Making the Application More User-Friendly
- •The IDE Components
- •The IDE Menu
- •The Toolbox Window
- •The Solution Explorer
- •The Properties Window
- •The Output Window
- •The Command Window
- •The Task List Window
- •Environment Options
- •A Few Common Properties
- •A Few Common Events
- •A Few Common Methods
- •Building a Console Application
- •Summary
- •Building a Loan Calculator
- •How the Loan Application Works
- •Designing the User Interface
- •Programming the Loan Application
- •Validating the Data
- •Building a Math Calculator
- •Designing the User Interface
- •Programming the MathCalculator App
- •Adding More Features
- •Exception Handling
- •Taking the LoanCalculator to the Web
- •Working with Multiple Forms
- •Working with Multiple Projects
- •Executable Files
- •Distributing an Application
- •VB.NET at Work: Creating a Windows Installer
- •Finishing the Windows Installer
- •Running the Windows Installer
- •Verifying the Installation
- •Summary
- •Variables
- •Declaring Variables
- •Types of Variables
- •Converting Variable Types
- •User-Defined Data Types
- •Examining Variable Types
- •Why Declare Variables?
- •A Variable’s Scope
- •The Lifetime of a Variable
- •Constants
- •Arrays
- •Declaring Arrays
- •Initializing Arrays
- •Array Limits
- •Multidimensional Arrays
- •Dynamic Arrays
- •Arrays of Arrays
- •Variables as Objects
- •So, What’s an Object?
- •Formatting Numbers
- •Formatting Dates
- •Flow-Control Statements
- •Test Structures
- •Loop Structures
- •Nested Control Structures
- •The Exit Statement
- •Summary
- •Modular Coding
- •Subroutines
- •Functions
- •Arguments
- •Argument-Passing Mechanisms
- •Event-Handler Arguments
- •Passing an Unknown Number of Arguments
- •Named Arguments
- •More Types of Function Return Values
- •Overloading Functions
- •Summary
- •The Appearance of Forms
- •Properties of the Form Control
- •Placing Controls on Forms
- •Setting the TabOrder
- •VB.NET at Work: The Contacts Project
- •Anchoring and Docking
- •Loading and Showing Forms
- •The Startup Form
- •Controlling One Form from within Another
- •Forms vs. Dialog Boxes
- •VB.NET at Work: The MultipleForms Project
- •Designing Menus
- •The Menu Editor
- •Manipulating Menus at Runtime
- •Building Dynamic Forms at Runtime
- •The Form.Controls Collection
- •VB.NET at Work: The DynamicForm Project
- •Creating Event Handlers at Runtime
- •Summary
- •The TextBox Control
- •Basic Properties
- •Text-Manipulation Properties
- •Text-Selection Properties
- •Text-Selection Methods
- •Undoing Edits
- •VB.NET at Work: The TextPad Project
- •Capturing Keystrokes
- •The ListBox, CheckedListBox, and ComboBox Controls
- •Basic Properties
- •The Items Collection
- •VB.NET at Work: The ListDemo Project
- •Searching
- •The ComboBox Control
- •The ScrollBar and TrackBar Controls
- •The ScrollBar Control
- •The TrackBar Control
- •Summary
- •The Common Dialog Controls
- •Using the Common Dialog Controls
- •The Color Dialog Box
- •The Font Dialog Box
- •The Open and Save As Dialog Boxes
- •The Print Dialog Box
- •The RichTextBox Control
- •The RTF Language
- •Methods
- •Advanced Editing Features
- •Cutting and Pasting
- •Searching in a RichTextBox Control
- •Formatting URLs
- •VB.NET at Work: The RTFPad Project
- •Summary
- •What Is a Class?
- •Building the Minimal Class
- •Adding Code to the Minimal Class
- •Property Procedures
- •Customizing Default Members
- •Custom Enumerations
- •Using the SimpleClass in Other Projects
- •Firing Events
- •Shared Properties
- •Parsing a Filename String
- •Reusing the StringTools Class
- •Encapsulation and Abstraction
- •Inheritance
- •Inheriting Existing Classes
- •Polymorphism
- •The Shape Class
- •Object Constructors and Destructors
- •Instance and Shared Methods
- •Who Can Inherit What?
- •Parent Class Keywords
- •Derived Class Keyword
- •Parent Class Member Keywords
- •Derived Class Member Keyword
- •MyBase and MyClass
- •Summary
- •On Designing Windows Controls
- •Enhancing Existing Controls
- •Building the FocusedTextBox Control
- •Building Compound Controls
- •VB.NET at Work: The ColorEdit Control
- •VB.NET at Work: The Label3D Control
- •Raising Events
- •Using the Custom Control in Other Projects
- •VB.NET at Work: The Alarm Control
- •Designing Irregularly Shaped Controls
- •Designing Owner-Drawn Menus
- •Designing Owner-Drawn ListBox Controls
- •Using ActiveX Controls
- •Summary
- •Programming Word
- •Objects That Represent Text
- •The Documents Collection and the Document Object
- •Spell-Checking Documents
- •Programming Excel
- •The Worksheets Collection and the Worksheet Object
- •The Range Object
- •Using Excel as a Math Parser
- •Programming Outlook
- •Retrieving Information
- •Recursive Scanning of the Contacts Folder
- •Summary
- •Advanced Array Topics
- •Sorting Arrays
- •Searching Arrays
- •Other Array Operations
- •Array Limitations
- •The ArrayList Collection
- •Creating an ArrayList
- •Adding and Removing Items
- •The HashTable Collection
- •VB.NET at Work: The WordFrequencies Project
- •The SortedList Class
- •The IEnumerator and IComparer Interfaces
- •Enumerating Collections
- •Custom Sorting
- •Custom Sorting of a SortedList
- •The Serialization Class
- •Serializing Individual Objects
- •Serializing a Collection
- •Deserializing Objects
- •Summary
- •Handling Strings and Characters
- •The Char Class
- •The String Class
- •The StringBuilder Class
- •VB.NET at Work: The StringReversal Project
- •VB.NET at Work: The CountWords Project
- •Handling Dates
- •The DateTime Class
- •The TimeSpan Class
- •VB.NET at Work: Timing Operations
- •Summary
- •Accessing Folders and Files
- •The Directory Class
- •The File Class
- •The DirectoryInfo Class
- •The FileInfo Class
- •The Path Class
- •VB.NET at Work: The CustomExplorer Project
- •Accessing Files
- •The FileStream Object
- •The StreamWriter Object
- •The StreamReader Object
- •Sending Data to a File
- •The BinaryWriter Object
- •The BinaryReader Object
- •VB.NET at Work: The RecordSave Project
- •The FileSystemWatcher Component
- •Properties
- •Events
- •VB.NET at Work: The FileSystemWatcher Project
- •Summary
- •Displaying Images
- •The Image Object
- •Exchanging Images through the Clipboard
- •Drawing with GDI+
- •The Basic Drawing Objects
- •Drawing Shapes
- •Drawing Methods
- •Gradients
- •Coordinate Transformations
- •Specifying Transformations
- •VB.NET at Work: Plotting Functions
- •Bitmaps
- •Specifying Colors
- •Defining Colors
- •Processing Bitmaps
- •Summary
- •The Printing Objects
- •PrintDocument
- •PrintDialog
- •PageSetupDialog
- •PrintPreviewDialog
- •PrintPreviewControl
- •Printer and Page Properties
- •Page Geometry
- •Printing Examples
- •Printing Tabular Data
- •Printing Plain Text
- •Printing Bitmaps
- •Using the PrintPreviewControl
- •Summary
- •Examining the Advanced Controls
- •How Tree Structures Work
- •The ImageList Control
- •The TreeView Control
- •Adding New Items at Design Time
- •Adding New Items at Runtime
- •Assigning Images to Nodes
- •Scanning the TreeView Control
- •The ListView Control
- •The Columns Collection
- •The ListItem Object
- •The Items Collection
- •The SubItems Collection
- •Summary
- •Types of Errors
- •Design-Time Errors
- •Runtime Errors
- •Logic Errors
- •Exceptions and Structured Exception Handling
- •Studying an Exception
- •Getting a Handle on this Exception
- •Finally (!)
- •Customizing Exception Handling
- •Throwing Your Own Exceptions
- •Debugging
- •Breakpoints
- •Stepping Through
- •The Local and Watch Windows
- •Summary
- •Basic Concepts
- •Recursion in Real Life
- •A Simple Example
- •Recursion by Mistake
- •Scanning Folders Recursively
- •Describing a Recursive Procedure
- •Translating the Description to Code
- •The Stack Mechanism
- •Stack Defined
- •Recursive Programming and the Stack
- •Passing Arguments through the Stack
- •Special Issues in Recursive Programming
- •Knowing When to Use Recursive Programming
- •Summary
- •MDI Applications: The Basics
- •Building an MDI Application
- •Built-In Capabilities of MDI Applications
- •Accessing Child Forms
- •Ending an MDI Application
- •A Scrollable PictureBox
- •Summary
- •What Is a Database?
- •Relational Databases
- •Exploring the Northwind Database
- •Exploring the Pubs Database
- •Understanding Relations
- •The Server Explorer
- •Working with Tables
- •Relationships, Indices, and Constraints
- •Structured Query Language
- •Executing SQL Statements
- •Selection Queries
- •Calculated Fields
- •SQL Joins
- •Action Queries
- •The Query Builder
- •The Query Builder Interface
- •SQL at Work: Calculating Sums
- •SQL at Work: Counting Rows
- •Limiting the Selection
- •Parameterized Queries
- •Calculated Columns
- •Specifying Left, Right, and Inner Joins
- •Stored Procedures
- •Summary
- •How About XML?
- •Creating a DataSet
- •The DataGrid Control
- •Data Binding
- •VB.NET at Work: The ViewEditCustomers Project
- •Binding Complex Controls
- •Programming the DataAdapter Object
- •The Command Objects
- •The Command and DataReader Objects
- •VB.NET at Work: The DataReader Project
- •VB.NET at Work: The StoredProcedure Project
- •Summary
- •The Structure of a DataSet
- •Navigating the Tables of a DataSet
- •Updating DataSets
- •The DataForm Wizard
- •Handling Identity Fields
- •Transactions
- •Performing Update Operations
- •Updating Tables Manually
- •Building and Using Custom DataSets
- •Summary
- •An HTML Primer
- •HTML Code Elements
- •Server-Client Interaction
- •The Structure of HTML Documents
- •URLs and Hyperlinks
- •The Basic HTML Tags
- •Inserting Graphics
- •Tables
- •Forms and Controls
- •Processing Requests on the Server
- •Building a Web Application
- •Interacting with a Web Application
- •Maintaining State
- •The Web Controls
- •The ASP.NET Objects
- •The Page Object
- •The Response Object
- •The Request Object
- •The Server Object
- •Using Cookies
- •Handling Multiple Forms in Web Applications
- •Summary
- •The Data-Bound Web Controls
- •Simple Data Binding
- •Binding to DataSets
- •Is It a Grid, or a Table?
- •Getting Orders on the Web
- •The Forms of the ProductSearch Application
- •Paging Large DataSets
- •Customizing the Appearance of the DataGrid Control
- •Programming the Select Button
- •Summary
- •How to Serve the Web
- •Building a Web Service
- •Consuming the Web Service
- •Maintaining State in Web Services
- •A Data-Driven Web Service
- •Consuming the Products Web Service in VB
- •Summary

504 Chapter 11 STORING DATA IN COLLECTIONS
The SortedList Class
The SortedList collection is a peculiar combination of the Array and HashTable classes. It maintains a list of items, which can be accessed either with an index or with a key. Moreover, the collection is always sorted according to the keys. The items of a SortedList are ordered according to the values of their keys, and there’s no method for sorting the collection according to the values stored in it.
To create a new SortedList collection, use a statement like the following:
Dim sList As New SortedList
As you may have guessed, this collection can store keys that are of the base data types. If you want to use custom objects as keys, you must specify an argument of the IComparer type, which tells VB how to compare the custom items. This information is crucial; without it, the SortedList won’t be able to maintain its items sorted. You can still store items in the SortedList, but they will appear in the order in which they were added.
This form of the SortedList constructor has the following syntax:
Dim sList As New SortedList(New comparer)
where comparer is the name of a custom IComparer interface (which is discussed in detail later in this chapter). There are also two more forms of the constructor, which allow you to specify the initial capacity of the SortedList collection, as well as a Dictionary object, whose data (keys and values) will be added to the SortedList.
Like the other two collections examined in this chapter, the SortedList collection supports the Capacity and Count properties. To add an item to a SortedList collection, use the Add method, whose syntax is
sList.Add(key, item)
where key is the key of the new item and item is the item to be added. Both arguments are objects. The Add method is the only way to add items to a SortedList collection. All items are inserted into the collection according to their keys, and each item’s key must be unique. Attempting to add a duplicate key will throw an exception.
The SortedList class also exposes the ContainsKey and ContainsValue properties, which allow you to find out whether a key or item exists in the list already. To add a new item, use the following statement that makes sure the key isn’t in use:
If Not sList.ContainsKey(myKey) Then
sList.Add(myKey, myItem)
End If
(Just replace myKey and myItem with your key and item.) It’s OK to store duplicate items in the same SortedList collection, but you can still detect the presence of an item in the list with a similar If statement.
To replace an existing item, use the SetByIndex method, which replaces the value at a specific index. The syntax of the method is
sList.SetByIndex(index, item)
where the first argument is the index at which the value will be inserted and item is the new item to be inserted in the collection. This object will replace the value that corresponds to the specified index.
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |

THE SORTEDLIST CLASS 505
The key, however, remains the same. There’s no equivalent method for replacing a key; you must first remove the item, and then insert it again with its new key.
To remove items from the collection, use the Remove and RemoveAt methods. The Remove method accepts a key as argument and removes the item that corresponds to that key. The RemoveAt method accepts an index as argument and removes the item at the specified index. To remove all the items from a SortedList collection, call its Clear method. After clearing the collection, you should also call its TrimToSize method to restore its capacity to the default size (16).
Let’s build a SortedList and print out its elements. The following listing declares the sList SortedList and then adds 10 items to the collection. The keys are integers, and the values are strings. The items are added in no specific order, but as soon as they’re added they’re inserted at the proper location in the collection, so that their keys are in ascending order.
Create a new project, place a button on its form, and enter Listing 11.11 in its Click event handler. The project you’ll build in this section is called SortedList, and you can find it on the CD.
Listing 11.11: Populating a Simple SortedList
Public Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs)
Dim sList As New System.Collections.SortedList()
‘Populate sortedlist sList.Add(16, “item 3”) sList.Add(10, “item 9”) sList.Add(15, “item 4”) sList.Add(17, “item 2”) sList.Add(11, “item 8”) sList.Add(14, “item 5”) sList.Add(18, “item 1”) sList.Add(12, “item 7”) sList.Add(19, “item 0”) sList.Add(13, “item 6”)
Dim SLEnum As IDictionaryEnumerator SLEnum = sList.GetEnumerator()
‘Print all key-value pairs While SLEnum.MoveNext
Console.WriteLine(“Key = “ & SLEnum.Key.Tostring & “, Value= “ & _
SLEnum.Value.ToString
End While
End Sub
The first segment of the code populates the ArrayList, while the second segment of the code prints all the key–value pairs in the order in which the enumerator retrieves them. The enumerator is the built-in mechanism for scanning a collection’s items (it will be discussed in detail later in this chapter).
If you execute these statements, they will produce the following output:
Key = 10, Value= item 9
Key = 11, Value= item 8
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |

506 Chapter 11 STORING DATA IN COLLECTIONS
Key = 12, Value= item 7
Key = 13, Value= item 6
Key = 14, Value= item 5
Key = 15, Value= item 4
Key = 16, Value= item 3
Key = 17, Value= item 2
Key = 18, Value= item 1
Key = 19, Value= item 0
The items are sorted according to their keys, regardless of the order in which they were inserted into the collection.
Let’s look now at a few methods for extracting keys and values. To find out the index of a value in the SortedList, use the IndexOfValue method, which accepts as argument an object. If the object exists in the collection, it returns its index. If not, it returns the value –1. If the same value appears more than once in the collection, the IndexOfValue property will return the first instance of the value. Moreover, there’s no mechanism for retrieving the following instances. Notice that the IndexOfValue property performs a case-sensitive search. The following statement will return the index 2 (the item you’re looking for is in the third place in the original SortedList):
Console.WriteLine(sList.IndexOfValue(“item 7”))
You can also find out the index of a specific key, with the IndexOfKey method, whose syntax is similar. Instead of a value, it locates a key. The following statement will return the index 7 (the key you’re looking for is in the eighth place in the SortedList):
Console.WriteLine(sList.IndexOfKey(17))
The GetKey and GetValue methods allow you to retrieve the index that corresponds to a specific key or value in the SortedList. Both methods accept an object as argument and return an index.
Finally, you can combine the two methods to retrieve the key that corresponds to a value, with a statement like the following one:
Console.WriteLine(sList.GetKey(sList.IndexOfValue(“item 7”)))
This statement will print the value 12, based on the contents of the sList collection in Listing 11.11.
Note If either the key or the value you’re searching for can’t be found, the IndexOfKey and IndexOfValue methods will return –1.
You can retrieve the keys in a SortedList collection and create another list, with the GetKeyList method. Likewise, the GetValueList method returns all the values in the SortedList. The following code extracts the keys from the sList SortedList and stores them in the keys list. Then, it scans the list with the help of the key variable and prints all the keys:
Dim keys As IList
keys = slist.GetKeyList() Dim key As Integer
For Each key In Keys Console.WriteLine(key)
Next
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |

THE SORTEDLIST CLASS 507
You can also extract both the keys and the values from a SortedList and store them into an ArrayList, as shown here:
Dim AllKeys As New ArrayList()
AllKeys.InsertRange(0, sList.GetValueList)
Each item is stored at a specific location in the SortedList, and you can find out the location of each item with a loop like the following:
Dim idx As Integer
For idx = 0 To sList.Count - 1
Console.WriteLine(“ITEM: “ & sList.GetByIndex(idx).ToString & _ “ is at location “ & idx.Tostring)
Next
The output produced by this code segment is:
ITEM: item 9 is at location 0
ITEM: item 8 is at location 1
ITEM: item 7 is at location 2
ITEM: item 6 is at location 3
ITEM: item 5 is at location 4
ITEM: item 4 is at location 5
ITEM: item 3 is at location 6
ITEM: item 2 is at location 7
ITEM: item 1 is at location 8
ITEM: item 0 is at location 9
You can also find out the location of each key, with a loop like the following one:
For idx = 0 To sList.Count - 1
Console.WriteLine(“The key at location “ & idx.ToString & “ is “ & _ sList.GetKey(idx).ToString)
Next
The output produced by the preceding code segment is:
The key at location 0 is 10
The key at location 1 is 11
The key at location 2 is 12
The key at location 3 is 13
The key at location 4 is 14
The key at location 5 is 15
The key at location 6 is 16
The key at location 7 is 17
The key at location 8 is 18
The key at location 9 is 19
Notice that the keys are rearranged as they’re added to the list, and they’re always physically sorted. As you can understand, the keys must be of a base data type. If not, the SortedList can’t compare the keys and therefore can’t maintain the proper order. To use objects as keys, you must also supply a function custom comparer (a function that knows how to compare two objects). The topic of creating
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |