In the previous sections, we used variables to store individual values. As a matter of fact, most programs store sets of data of different types. For example, a program for balancing your checkbook must store several pieces of information for each check: the check’s number, amount, date, and so on. All these pieces of information are necessary to process the checks, and ideally, they should be stored together.
You can create custom data types that are made up of multiple values using structures. A VB structure allows you to combine multiple values of the basic data types and handle them as a whole. For example, each check in a checkbook-balancing application is stored in a separate structure (or record), as shown in Figure 2.3. When you recall a given check, you need all the information stored in the structure.
Figure 2.3 – Pictorial representation of a structure
To define a structure in VB 2008, use the Structure statement, which has the following syntax:
Structure structureName
Dim variable1 As varType
Dim variable2 As varType
...
Dim variablen As varType
End Structure
Code language: VB.NET (vbnet)
varType can be any of the data types supported by the CLR. The Dim statement can be replaced by the Private or Public access modifiers. For structures, Dim is equivalent to Public.
After this declaration, you have in essence created a new data type that you can use in your application. structureName can be used anywhere you’d use any of the base types (Integers, Doubles, and so on). You can declare variables of this type and manipulate them as you manipulate all other variables (with a little extra typing). The declaration for the CheckRecord structure shown in Figure 2.3 is as follows:
Structure CheckRecord
Dim CheckNumber As Integer
Dim CheckDate As Date
Dim CheckAmount As Single
Dim CheckPaidTo As String
End Structure
Code language: VB.NET (vbnet)
This declaration must appear outside any procedure; you can’t declare a Structure in a subroutine or function. Once declared, The CheckRecord structure becomes a new data type for your application.
To declare variables of this new type, use a statement such as this one:
Dim check1 As CheckRecord, check2 As CheckRecord
Code language: VB.NET (vbnet)
To assign a value to one of these variables, you must separately assign a value to each one of its components (they are called fields), which can be accessed by combining the name of the variable and the name of a field, separated by a period, as follows:
check1.CheckNumber = 275
Code language: VB.NET (vbnet)
Actually, as soon as you type the period following the variable’s name, a list of all members to the CheckRecord structure will appear, as shown in Figure 2.4. Notice that the structure supports a few members on its own.
Figure 2.4 – Variables of custom types expose their members as properties
You didn’t write any code for the Equals, GetType, and ToString members, but they’re standard members of any Structure object, and you can use them in your code. Both the GetType and ToString methods will return a string like ProjectName.FormName + CheckRecord. You can provide your own implementation of the ToString method, which will return a more meaningful string:
Public Overrides Function ToString() As String
Return "CHECK # " & CheckNumber & " FOR " &
CheckAmount.ToString("C")
End Function
Code language: VB.NET (vbnet)
As you understand, structures are a lot like objects that expose their fields as properties and then expose a few members of their own. The following statements initialize a CheckRecord variable:
check2.CheckNumber = 275
check2.CheckDate = #09/12/2008#
check2.CheckAmount = 104.25
check2.CheckPaidTo = "Gas Co."
Code language: VB.NET (vbnet)
You can also create arrays of structures with a declaration such as the following (arrays are discussed later in this chapter):
Dim Checks(100) As CheckRecord
Code language: VB.NET (vbnet)
Each element in this array is a CheckRecord structure and it holds all the fields of a given check. To access the fields of the third element of the array, use the following notation:
Checks(2).CheckNumber = 275
Checks(2).CheckDate = #09/12/2008#
Checks(2).CheckAmount = 104.25
Checks(2).CheckPaidTo = "Gas Co."
Code language: VB.NET (vbnet)
The Nothing Value
The Nothing value is used with object variables and indicates a variable that has not been initialized. If you want to disassociate an object variable from the object it represents, set it to Nothing. The following statements create an object variable that references a brush, uses it, and then releases it:
Dim brush As SolidBrush
brush = New SolidBrush(Color.Blue)
{ use brush object to draw with}
brush = Nothing
Code language: VB.NET (vbnet)
The first statement declares the brush variable. At this point, the brush variable is Nothing. The second statement initializes the brush variable with the appropriate constructor (the brush is initialized to a specific color). After the execution of the second statement, the brush variable actually represents an object you can draw with in blue. After using it to draw something, you can release it by setting it to Nothing.
If you want to find out whether an object variable has been initialized, use the Is or IsNot operators, as shown in the following example:
Dim myPen As Pen
{ more statements here}
If myPen Is Nothing Then
myPen = New Pen(Color.Red)
End If
Code language: VB.NET (vbnet)
The variable myPen is initialized with the New constructor only if it hasn’t been initialized already. If you want to release the myPen variable later in your code, you can set it to Nothing with the assignment operator. When you compare an object to Nothing, however, you can’t use the equals operator; you must use the Is and IsNot operators.
Examining Variable Types
Besides setting the types of variables and the functions for converting between types, Visual Basic provides the GetType method, which returns a string with the variable’s type (Int32, Decimal, and so on). Any variable exposes these methods automatically, and you can call them like this:
Dim var As Double
Debug.WriteLine "The variable's type is " & var.GetType.ToString
Code language: VB.NET (vbnet)
There’s also a GetType operator, which accepts as an argument a type and returns a Type object for the specific data type. The GetType method and GetType operator are used mostly in If structures, like the following one:
If var.GetType() Is GetType(Double) Then
{ code to handle a Double value}
End If
Code language: VB.NET (vbnet)
Notice that the code doesn’t reference data type names directly. Instead, it uses the value returned by the GetType operator to retrieve the type of the class System.Double and then compares this value to the variable’s type with the Is (or the IsNot) keyword.