Visual Basic .NET Power Tools

Team Fly 

Page 28

TIP VB.NET creates the event in the code window for you, if you wish. Your btnSearch doesn't show up in the Design window, so you cannot double-click it there to force VB.NET to create a Click event for it. However, you can use the dropdown lists. After you have declared a control WithEvents (Dim WithEvents btnSearch As New Button()), drop the list in the top left of the code window, and locate btnSearch. Click it to select it. Then drop the list in the top right, and double-click the event that you want VB.NET to create for you in the code window.

TIP Each form has a collection that includes all the controls on that form. You access the collection, as illustrated previously, using Me.Controls or simply Controls. The collection can be added to, as shown in the previous example, or can be subtracted from Me.Controls.Remove(Button1).

Note, too, that the Me.Controls collection also has several other methods: Clear, Equals, GetChildIndex, GetEnumerator, GetHashCode, GetType, SetChildIndex, ShouldPersistAll, and ToString. There are also three properties available to Me.Controls: Count, Item, and IsReadOnly.

Using Arrays

You probably should familiarize yourself with all the new, significant members available in VB.NET for the collection classes, including the various kinds of arrays.

Arrays can now contain objects (technically, that's all they now contain) and can search and sort themselves, and the new ArrayList class is especially worthwhile.

Zero-Based Collections (Sometimes)

Arrays are always zero-based in the .NET Framework. In classic VB you could use the Option Base statement to allow arrays to start with element 1 instead of 0. Option Base has been deleted from VB.

Therefore, you must wrestle with the artificial distinction between dimension (the size you declare) and capacity (the number of elements). For decades now, programmers have had to fiddle with their loop values to fix this silly distinction:

 Dim a(3) As String For i = 0 To a.Length - 1 

Because dimensioning this array as 3 actually creates 4 elements, you must therefore subtract 1 from your loop counter.

We humans always count up from 1 when dealing with collections (lists, sets, groups, and so on). It's natural to our way of describing, and therefore thinking about, numbers. When the first person arrives at your BBQ, you don't say ''Welcome, you're the zeroth one here!" And when your child is one year old, you don't send out invitations titled "Jimmy's Zeroth Birthday Party!!" We quite properly think of zero as meaning nothing—absence, nonexistence.

You've doubtless had to fiddle around with this foolishness many times in your programming career. The old familiar error message, "An unhandled exception of type 'System.IndexOutOfRangeException' occurred...," has been unnecessarily triggered millions of times. Unnecessarily because mathematical diction, fundamental logic, elementary grammar, and simple common sense all require that lists begin with the first (not the zeroth) item. Computer languages, though, are designed by a certain kind of committee—a group that does not invite language specialists, such as English majors, to the table.

Team Fly 


Visual Basic  .NET Power Tools
Visual Basic .NET Power Tools
ISBN: 0782142427
EAN: 2147483647
Year: 2003
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net