User-Defined Data Types - Variables in Visual Basic 2008
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 checktutorial-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.
To define a structure in VB 2008, use the Structure statement, which has the following syntax:
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:
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:
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:
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.
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:
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:
You can also create arrays of structures with a declaration such as the following (arrays are discussed later in this chapter):
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:
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:
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:
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.
Table of Contents
W3computing.com Copyright 2011 © All Rights Reserved