INITIALIZING COLLECTIONS

Collection classes that provide an Add method such as List, Dictionary, and SortedDictionary have their own initialization syntax. Instead of using an equals sign as you would with an array initializer, use the From keyword followed by the values that should be added to the collection surrounded by curly braces.

For example, the following code initializes a new List(Of String):

Dim pies As New List(Of String) From
    {
        "Apple", "Banana", "Cherry", "Coconut Cream"
    }

The items inside the braces must include all of the values needed by the collection’s Add method. For example, the Dictionary class’s Add method takes two parameters giving the key and value that should be added so each entry in the initializer should include a key and value.

The following code initializes a Dictionary(Of String, String). The parameters to the class’s Add method are an item’s key and value so, for example, the value 940-283-1298 has the key Alice Artz. Later you could look up Alice’s phone number by searching the Dictionary for the item with key “Alice Artz.”

Dim phone_numbers As New Dictionary(Of String, String) From
    {
        {"Alice Artz", "940-283-1298"},
        {"Bill Bland", "940-237-3827"},
        {"Carla Careful", "940-237-1983"}
    }

ADDING ADD
Some collection classes such as Stack and Queue don’t have an Add method, so From won’t work for them. Fortunately, you can use extension methods (described in the “Extension Methods” section in Chapter 16, “Subroutines and Functions”) to add one. The following code adds a simple extension method to the Stack (Of String) class:
<Extension()>
Public Sub Add(Of T)(the_stack As Stack(Of T), value As T)
    the_stack.Push(value)
End Sub
Now the program can initialize a Stack(Of String) as in the following code:
Dim orders As New Stack(Of String) From
    {
        "Art", "Beatrice", "Chuck"
    }

Multiple Variable Declarations

Visual Basic .NET allows you to declare more than one variable in a single declaration statement. For example, the following statement declares two Integer variables named num_employees and num_customers:

Private num_employees, num_customers As Integer

You can place accessibility keywords (Private, Public, and so on), Shared, Shadows, and ReadOnly only at the beginning of the declaration and they apply to all of the variables in the declaration. In the preceding statement, both num_employees and num_customers are Private.

You can declare variables with different data types by including more than one As clause separated by commas. The following statement declares two Integer variables and one String variable:

Private emps, custs As Integer, cust As String

You cannot use an initialization statement if multiple variables share the same As clause, but you can include an initialization statement for variables that have their own As clauses. In the preceding example, you cannot initialize the two Integer variables, but you can initialize the String variable as shown in the following statement:

Private emps, custs As Integer, cust As String = "Cozmo"

To initialize all three variables, you would need to give them each their own As clauses, as shown in the following example:

Private emps As Integer = 5, custs As Integer = 10, cust As String = "Cozmo"

You can also declare and initialize multiple objects, arrays, and arrays of objects all in the same statement.

While all of these combinations are legal, they quickly become too confusing to be of much practical use. Even the relatively simple statement that follows can lead to later misunderstandings. Quickly glancing at this statement, the programmer may think that all three variables are declared as Long.

Private num_employees, num_customers As Integer, num_orders As Long

You can reduce the possibility of confusion by using one As clause per declaration. Then a programmer can easily understand how the variables are defined by looking at the beginning and ending of the declaration. The beginning tells the programmer the variables’ accessibility and whether they are shared, shadowing other variables, or read-only. The end gives the variables’ data type.

You can also keep the code simple by giving variables with initialization statements their own declarations. Then a programmer reading the code won’t need to decide whether an initialization statement applies to one or all of the variables.

There’s nothing particularly wrong with declaring a series of relatively short variables in a single statement, as long as you don’t find the code confusing. The following statements declare five Integer variables and three Single variables. Breaking this into eight separate Dim statements would not make it much clearer.

Dim i, j, k, R, C As Integer
Dim X, Y, Z As Single
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.8.133