Custom Types Collections Types

33 String The String type holds a sequence of Unicode characters. The underlying .NET type is System.String. Of the fundamental types, Boolean, Byte, Char, Date, Decimal, Double, Integer, Long, Short, and Single that is, all of them except Object and String are value types. Object and String are reference types.

2.5.2 Custom Types

Visual Basic .NET provides rich syntax for extending the type system. Programmers can define both new value types and new reference types. Types declared with Visual Basic .NETs Structure and Enum statements are value types, as are all .NET Framework types that derive from System.ValueType. Reference types include Object, String, all types declared with Visual Basic .NETs Class , Interface , and Delegate statements, and all .NET Framework types that dont derive from System.ValueType.

2.5.3 Arrays

Array declarations in Visual Basic .NET are similar to those in Visual Basic 6 and other languages. For example, here is a declaration of an Integer array that has five elements: Dim a4 As Integer The literal 4 in this declaration specifies the upper bound of the array. All arrays in Visual Basic .NET have a lower bound of , so this is a declaration of an array with five elements, having indexes 0, 1, 2, 3, and 4. The previous declaration is of a variable named a , which is of type array of Integer. Array types implicitly inherit from the .NET Frameworks Array type defined in the System namespace and, therefore, have access to the methods defined in that type. For example, the following code displays the lower and upper bounds of an array by calling the Array classs GetLowerBound and GetUpperBound methods: Dim a4 As Integer Console.WriteLineLowerBound is a.GetLowerBound0.ToString Console.WriteLineUpperBound is a.GetUpperBound0.ToString The output is: LowerBound is 0 UpperBound is 4 Note that the upper bound of the array is dynamic: it can be changed by methods available in the Array type. Array elements are initialized to the default value of the element type. A types default value is determined as follows: • For numeric types, the default value is . • For the Boolean type, the default value is False . • For the Char type, the default value is the character whose Unicode value is . 34 • For structure types described later in this chapter, the default value is an instance of the structure type with all of its fields set to their default values. • For enumeration types described later in this chapter, the default value is an instance of the enumeration type with its internal representation set to , which may or may not correspond to a legal value in the enumeration. • For reference types including String, the default value is Nothing . You can access array elements by suffixing the array name with the index of the desired element enclosed in parentheses, as shown here: For i = 0 To 4 Console.WriteLineai Next Arrays can be multidimensional. Commas separate the dimensions of the array when used in declarations and when accessing elements. Here is the declaration of a three-dimensional array, where each dimension has a different size: Dim a5, 10, 15 As Integer As with single-dimensional arrays, array elements are initialized to their default values.

2.5.3.1 Initializing arrays

Arrays of primitive types can be initialized by enclosing the initial values in curly brackets {} . For example: Dim a As String = {First, Second, Third, Fourth, Fifth} Notice that when arrays are initialized in this manner, the array declaration is not permitted to specify an explicit size. The compiler infers the size from the number of elements in the initializer. To initialize multidimensional arrays, include the appropriate number of commas in the array-name declaration and use nested curly brackets in the initializer. Here is a declaration of a two-dimensional array having three rows and two columns: Dim a, As Integer = {{1, 2}, {3, 4}, {5, 6}} This declaration produces the following array: a0,0=1 a0,1=2 a1,0=3 a1,1=4 a2,0=5 a2,1=6 When initializing multidimensional arrays, the innermost curly brackets correspond to the rightmost dimension.

2.5.3.2 Dynamically allocating arrays

Use the New keyword to allocate arrays of any type. For example, this code creates an array of five Integers and initializes the elements as shown: Dim a As Integer a = New Integer4 {1, 2, 3, 4, 5} 35 If the array elements wont be initialized by the allocation, it is still necessary to include the curly brackets: Dim a As Integer allocates an uninitialized array of five Integers a = New Integer5 {} Curly brackets are required so the compiler wont confuse the array syntax with constructor syntax. Note also the meaning of this declaration by itself: Dim a As Integer This is the declaration of a reference that could point to a single-dimensional array of Integers, but doesnt yet. Its initial value is Nothing .

2.5.4 Collections

A collection is any type that exposes the ICollection interface defined in the System.Collections namespace. Interfaces are explained later in this chapter. Briefly, an interface is an agreement in which the type will expose certain methods, properties, and other members. By exposing the ICollection interface, a type ensures that it can be used anywhere a collection is expected. In general, collections store multiple values and provide a way for iterating through those values. Specialized collection types may also provide other means for adding and reading values. For example, the Stack type defined in the System.Collections namespace provides methods, such as Push and Pop, for performing operations that are appropriate for the stack data structure. The Visual Basic .NET runtime provides a type called Collection defined in the Microsoft.VisualBasic namespace that mimics the behavior of Visual Basic 6 collections and exposes the ICollection interface. Ex am ple 2- 1 shows its use. Example 2-1. Using the Collection type Create a new collection object. Dim col As New Collection Add some items to the collection. col.AddSome value col.AddSome other value col.AddA third value Iterate through the collection and output the strings. Dim obj As Object For Each obj In col Dim str As String = CTypeobj, String Console.WriteLinestr Next The Collection types Add method adds items to the collection. Although strings are added to the collection in Ex am ple 2- 2 , the Add method is defined to take items of type Object, meaning that any type can be passed to the method. After items are added to the collection, they can be iterated using the For Each statement discussed later in this chapter, under Sect ion 2.13 . Because the Collection class is defined to store items of type Object, the loop variable in the For Each statement must be of type Object. Because the items are actually strings, the code in Ex am ple 2- 1 converts the Object references to String references using the CType function. Type conversions are discussed later in this section. The output of the code in Ex am ple 2- 1 is: 36 Some value Some other value A third value The items in a Collection object can also be iterated using a numerical index. The Collection object has a Count property, which indicates the number of items in the collection. Ex am ple 2- 2 is precisely the same as Ex am ple 2- 1 , except that it iterates through the Collection object using a numerical index and a standard For loop. Example 2-2. Using a numerical index on a collection object Create a new collection object. Dim col As New Collection Add some items to the collection. col.AddSome value col.AddSome other value col.AddA third value Iterate through the collection and output the strings. Dim i As Integer For i = 1 To col.Count Dim str As String = CTypecoli, String Console.WriteLinestr Next Note that to access an item by index, the index number is placed within parentheses following the name of the Collection reference variable, as shown again here: coli The syntax of the Add method is: Public Sub Add _ ByVal Item As Object, _ Optional ByVal Key As String = Nothing, _ Optional ByVal Before As Object = Nothing, _ Optional ByVal After As Object = Nothing _ The parameters are: Item The item to add to the collection. Key An optional string value that can be used as an index to retrieve the associated item. For example, the following code adds an item to a collection and then uses the key value to retrieve the item: Dim col As New Collection col.AddSome value, Some key 37 ... Dim str As String = CTypecolSome key, String Console.WriteLinestr The output is: Some value Before The item before which the new item should be added. After The item after which the new item should be added. The .NET Framework class library provides several additional collection types, which are listed and briefly discussed in Chapt er 3 .

2.5.5 Type Conversions