Understanding Variables


Understanding Variables

Variables are simply places where information is temporarily stored while a program is running. A variable has a value only when you assign one to it, either through direct interaction (clicking a button on a dialog box, for example) or by the action of code in your program.

Variables are used when interacting with users, tracking details while the program is running, and passing information among different parts of the program. They are also used to represent individual items in a collection of items, such as when working one-by-one through a collection of tasks in a project.

A Boolean variable, for example, is declared as follows :

 Dim blnStatus As Boolean 

An array is a special type of variable that can store many related values at one time. (Because an array has to be declared like any other variable, all the data in an array has to be the same data type. The exception, of course, is an array declared as Variant.) In fact, an array can store a nearly unlimited number of values ”limited primarily by the amount of memory in your computer ”in up to 60 dimensions.

Arrays are a lot like temporary spreadsheets, in which each " cell " ( element ) in a "column" ( dimension ) is a single piece of data. Each time you add a dimension, it's like adding another column to your "spreadsheet." The final dimension in an array declaration (or the only dimension in a one-dimension array) is the number of rows in your spreadsheet.

Note  

By default, the dimensions in an array are 0-based, which means that the first element is numbered 0 instead of 1. When you declare the size of the array, you subtract1from the desired size to determine the upper bound of the array.

Examples of array declarations are shown in Table 30-2.

Table 30-2: Array Declarations

Description

Declaration

One-dimension Long array of fixed size (1 column with5rows)

Dim lngOneDArray(4) As Long

Two-dimension Integer array of fixed size (5 columns with 10 rows)

Dim intTwoDArray(4, 9) As Integer

Two-dimension Integer array of fixed size using 1-based (not zero-based ) dimensions

Dim intTwoDArray(1 To 5,1To 10) As Integer

One-dimension String array of variable size

Initial declaration: Dim strVarArray() As String Specification of initial size: ReDim strVarArray(4)
Specification of new size, while retaining existing data: ReDim Preserve strVarArray(9)

Two-dimension String array of variable size

Initial declaration: Dim strVarArray() As String Specification of initial size: ReDim strVarArray(4, 9)
Specification of new size, while discarding existing data: ReDim strVarArray(4, 19)

Constants are similar to variables in that they store data, but the value of a constant is specified when you declare it and never changes while your code is running. In this sense, they are like read-only properties.

At first glance, constants might not appear very useful, but they are valuable tools for writing self-documenting code. For example, the Microsoft Project constant "pjFinishToStart" is a much more meaningful description of the link type for a task than 1, which is the value it represents.

A typical String constant might be declared as:

 Const c_strName As String = "Some text" 
Cross-References  

For more information about self-documenting code, see "Writing Code That Is Easily Understood" later in this chapter.




Microsoft Office Project 2003 Inside Out
Microsoft Office Project 2003 Inside Out
ISBN: 0735619581
EAN: 2147483647
Year: 2003
Pages: 268

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