When you declare a variable or a named constant, Visual Basic reserves an area of memory and assigns it a name, called an identifier. You specify identifier names according to the rules of Basic as well as some recommended naming conventions. The declaration statements establish your project’s variables and constants, give them names, and specify the type of data they will hold. The statements are not considered executable; that is, they are not executed in the flow of instructions during program execution. Here are some sample declaration statements: Dim strName As String a string variable ‘Declare Dim intCounter As Integer an Integer variable ‘Declare Const curDiscountRate As Currency = .15 a named constant ‘Declare The next few paragraphs describe the data types, the rules for naming variables and constants, and the format of the declarations. Data Types The data type of a variable or constant indicates what type of information will be stored in the allocated memory space: perhaps a name, a dollar amount, a date, or a total. Note that the default data type is variant. If you do not specify a data type, your variables and constants will be variants. The advantage of using variant data type is that it’s easy, and the variables and constants change their appearance as needed for each situation. The disadvantage is that variants are less efficient that the other data types; that is, variants require more memory and operate less quickly that other data types. The best practice is to always specify the data type. Data Type Use For True or False values Boolean A single character (code 0 to 255). Byte Currency Decimal fractions, such as dollars and cents. Date An eight-character date. Double Double-precision floating-point numbers with 14 digits of accuracy. Integer Whole number in the range -32,768 to 32,767. Long Larger whole numbers. Single Single-precision numbers with six digits of accuracy. String Alphanumeric data: letters, digits, and other characters. Variant Converts from one type to another, as needed.
When you declare a variable or a named constant, Visual Basic reserves an area of memory and assigns it a name, called an identifier. You specify identifier names according to the rules of Basic as well as some recommended naming conventions. The declaration statements establish your project’s variables and constants, give them names, and specify the type of data they will hold. The statements are not considered executable; that is, they are not executed in the flow of instructions during program execution. Here are some sample declaration statements: Dim strName As String ‘Declare a string variable Dim intCounter As Integer ‘Declare an Integer variable Const curDiscountRate As Currency = .15 ‘Declare a named constant The next few paragraphs describe the data types, the rules for naming variables and constants, and the format of the declarations. Data Types The data type of a variable or constant indicates what type of information will be stored in the allocated memory space: perhaps a name, a dollar amount, a date, or a total. Note that the default data type is variant. If you do not specify a data type, your variables and constants will be variants. The advantage of using variant data type is that it’s easy, and the variables and constants change their appearance as needed for each situation. The disadvantage is that variants are less efficient that the other data types; that is, variants require more memory and operate less quickly that other data types. The best practice is to always specify the data type. Data Type Use For Boolean True or False values Byte A single character (code 0 to 255). Currency Decimal fractions, such as dollars and cents. Date An eight-character date. Double Double-precision floating-point numbers with 14 digits of accuracy. Integer Whole number in the range -32,768 to 32,767. Long Larger whole numbers. Single Single-precision numbers with six digits of accuracy. String Alphanumeric data: letters, digits, and other characters. Variant Converts from one type to another, as needed.
variables_constants_and_calculations.docx