Variables and Data Types You’ll learn how to do the following: ◆ Declare and use variables ◆ Use the native data types ◆ Use arrays
A variable stores data, which are processed with statements. A program is a list of statements that manipulate variables. In most programming languages, variables must be declared in advance. When programming in VB 2008, you should declare your variables because this is the default mode, and Microsoft recommends this practice strongly.
A variable’s name ◆ Must begin with a letter, followed by more letters or digits. ◆ Can’t contain embedded periods or other special punctuation symbols. The only special character that can appear in a variable’s name is the underscore character. ◆ Mustn’t exceed 255 characters. ◆ Must be unique within its scope. This means that you can’t have two identically named variables in the same subroutine, but you can have a variable named counter in many different subroutines.
Visual Basic recognizes the following five categories of variables: ◆ Numeric ◆ String ◆ Boolean ◆ Date ◆ Object The two major variable categories are numeric and string. Numeric variables store numbers, and string variables store text. Object variables can store any type of data.
Data Type Memory R e presentation S tore s Byte ( Byte ) 1 byte Integers in the range 0 to 255. Integer 4 bytes Integer values in the range − 2, 147, 483, 648 to 2, 147, 483, 647. Long ( Int 64 ) 8 bytes − 9, 223, 372, 036, 854, 755, 808 to Integer values in the range 9, 223, 372, 036, 854, 755, 807. Single Precision ( Single ) represent Double Precision ( Double ) numbers. It can 4 bytes Single-precision floating-point numbers. It can numbers in the range − 3. 402823 E 38 to 3. 402823 E 38. 8 bytes Double-precision floating-point represent numbers in the range − 1. 79769313486232 E 308 to 1. 79769313486232 E 308.
To declare a variable, use the Dim statement followed by the variable’s name, the As keyword, and its type, as follows: Dim meters As Integer Dim greetings As String The first variable, meters, will store integers, such as 3 or 1, 002; the second variable, greetings, will store text.
If you want to declare multiple variables of the same type, you need not repeat the type. Just separate all the variables of the same type with commas and set the type of the last variable: Dim Length, Width, Height As Integer, Volume, Area As Double
String Variables The String data type stores only text, and string variables are declared as follows: Dim some. Text As String You can assign any text to the variable some. Text. You can store nearly 2 GB of text in a string variable. The following assignments are all valid: Dim a. String As String a. String = ”Now is the time for all good men to come ” & ” to us” a. String = ”There approximately 25, 000 words in this chapter” a. String = ” 25, 000”
Date Variables Date are double-precision numbers: the integer part represents the date, and the fractional part represents the time. A variable declared as Date with a statement like the following can store both date and time values: Dim expiration As Date The following are all valid assignments: expiration = #01/01/2008# expiration = #8/27/2008 6: 29: 11 PM# expiration = ”July 2, 2008” expiration = Today()
Boolean Variables The Boolean data type stores True/False values. Boolean variables are, in essence, integers that take the value − 1 (for True) and 0 (for False). Actually, any nonzero value is considered True. Boolean variables are declared as Dim failure As Boolean
The Strict, Explicit, and Infer Options The Visual Basic compiler provides three options that determine how it handles variables: ◆ The Explicit option indicates whether you will declare all variables. ◆ The Strict option indicates whether all variables will be of a specific type. ◆ The Infer option indicates whether the compiler should determine the type of a variable from its value.
Data Type Identifiers Symbol Data Type $ String A$ , % Integer & Long ! Single # Double @ Decimal Example message. Text$ counter% , var% population& , color. Value& distance! Exact. Distance # Balance@
Constants Some variables don’t change value during the execution of a program. These variables are constants that appear many times in your code. The manner in which you declare constants is similar to the manner in which you declare variables, except that you use the Const keyword and in addition to supplying the constant’s name, you must also supply a value, as follows: Const constantname As type = value Const Exp. Date = #31/12/1997#
Arrays An array is similar to a variable: It has a name and multiple values. Each value is identified by an index (an integer value) that follows the array’s name in parentheses. Each different value is an element of the array. If the array Salaries holds the salaries of 16 employees, the element Salaries(0) holds the salary of the first employee, the element Salaries(1) holds the salary of the second employee, and so on up to the element Salaries(15).
Declaring Arrays Unlike simple variables, arrays must be declared with the Dim (or Public) statement followed by the name of the array and the index of the last element in the array in parentheses — Dim Salary(15) As Integer Dim Names(15) As String Names(0) = ”Joe Doe” Salary(0) = 34000 Names(1) = ”Beth York” Salary(1) = 62000. . . Names(15) = ”Peter Smack” Salary(15) = 10300
Dynamic Arrays Sometimes you may not know how large to make an array. The size of a dynamic array can vary during the course of the program. To create a dynamic array, declare it as usual with the Dim statement (or Public or Private), but don’t specify its dimensions. Later in the program, when you know how many elements you want to store in the array, use the Re. Dim statement to redimension the array, this time to its actual size.
In the following example, User. Count is a user-entered value: Dim Dyn. Array() As Integer ……. Re. Dim Dyn. Array(User. Count)
Visual Basic Order of Operator Precedence Exponentiation (^) Addition and subtraction(+, –) Negation (–) Multiplication and division (*, /) Integer division () Modulus arithmetic (Mod) String concatenation (&)
Not And Or All comparison operators, such as >, <, and = (discussed in the next section), have equal precedence.
Formatting Dates and Times The basic Format() function has the following syntax: Format(expression, style) The parameter expression is the expression to format, and style is a string specifying the formatting.
Format(#1/22/2009#, "MM") ' Returns 01 Format(#1/22/2009#, "MMM") ' Returns Jan Format(#1/22/2009#, "MMMM") ' Returns January