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 subtract 1 from 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



One-dimension Long array of fixed

Dim lngOneDArray(4) As Long

size (1 column with 5 rows)

Two-dimension Integer array of fixed

Dim intTwoDArray(4, 9) As Integer

size (5 columns with 10 rows)

Two-dimension Integer array of fixed Dim intTwoDArray(1 To 5, 1 To 10) As Integer ®

size using 1-based (not zero-based) j|

dimensions °

Table 30-2. Array Declarations



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"

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

For all intents and purposes, everything that happens in your code happens in procedures. Functions and subroutines (subs) are both types of procedures you can write. Both contain Visual Basic code (statements), and both can accept input (arguments).

The only real difference between functions and subroutines is that functions can directly return information to the calling procedure by assigning a value to the name of the function. This means you can use functions like variables, so functions should be declared as a certain data type, just like any other variable.

For example, the following code has a sub AddTask that calls a function strNewTask:

Function strNewTask(strName As String, intBefore As Integer) As String ActiveProject.Tasks.Add strName, intBefore

If ActiveProject.Tasks(intBefore).Name = strName Then strNewTask = "Success!"

Else strNewTask = "Failed to create task!" End If End Function

Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook

Post a comment