In Visual Basic, as in any other programming language, variables store values during a program's execution. A variable has a name and a value. The variable UserName, for example, can have the value Joe, and the variable Discount can have the value … [Read more...] about Variables in Visual Basic
Using Variables and Data Types
Types of Variables
Visual Basic recognizes the following five categories of variables: NumericStringBooleanDateObject The two major variable categories are numeric and string. Numeric variables store numbers, and string variables store text. Object variables can … [Read more...] about Types of Variables
Integer Variables
There are three types of variables for storing integers, and they differ only in the range of numbers each can represent. As you understand, the more bytes a type takes, the larger values it can hold. The type of Integer variable you'll use depends … [Read more...] about Integer Variables
Single-Precision and Double-Precision Numbers
The names Single and Double come from single-precision and double-precision numbers. Double-precision numbers are stored internally with greater accuracy than single-precision numbers. In scientific calculations, you need all the precision you can … [Read more...] about Single-Precision and Double-Precision Numbers
The Decimal Data Type
Variables of the Decimal type are stored internally as integers in 16 bytes and are scaled by a power of 10. The scaling power determines the number of decimal digits to the right of the floating point, and it's an integer value from 0 to 28. When … [Read more...] about The Decimal Data Type
Infinity and Other Oddities
The Framework can represent two very special values, which may not be numeric values themselves but are produced by numeric calculations: NaN (not a number) and Infinity. If your calculations produce NaN or Infinity, you should confirm the data and … [Read more...] about Infinity and Other Oddities
Byte Variables
None of the previous numeric types is stored in a single byte. In some situations, however, data are stored as bytes, and you must be able to access individual bytes. The Byte data type holds an integer in the range of 0 to 255. Bytes are frequently … [Read more...] about Byte Variables
Boolean and String 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 and they are … [Read more...] about Boolean and String Variables
Character Variables
Character variables store a single Unicode character in two bytes. In effect, characters are Unsigned Short integers (UInt16); you can use the CChar() function to convert integers to characters and use the CInt() function to convert characters to … [Read more...] about Character Variables
Date Variables
Date and time values are stored internally in a special format, but you don't need to know the exact format. They are double-precision numbers: the integer part represents the date, and the fractional part represents the time. A variable declared as … [Read more...] about Date Variables
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 … [Read more...] about The Strict, Explicit, and Infer Options
Object Variables
Variants — variables without a fixed data type— were the bread and butter of VB programmers up to version 6. Variants are the opposite of strictly typed variables: They can store all types of values, from a single character to an object. If you're … [Read more...] about Object Variables
Variables as Objects
Variables in Visual Basic 2008 are more than just names or placeholders for values. They're intelligent entities that can not only store but also process their values. I don't mean to confuse you all, but I think you must be told: Visual Basic 2008 … [Read more...] about Variables as Objects
Converting Variable Types
In many situations, you will need to convert variables from one type into another. Table 2.4 shows the methods of the Convert class that perform data-type conversions. In addition to the methods of the Convert class, you can still use the … [Read more...] about Converting Variable Types
Formatting Numbers
So far, you've seen how to use the basic data types of the CLR. All data types expose a ToString method, which returns the variable's value (a number or date) as a string, so that it can be used with other strings in your code. The ToString method … [Read more...] about Formatting Numbers
User-Defined Data Types
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 … [Read more...] about User-Defined Data Types
IsNumeric(), Isdate() and IsArray() Functions
Another set of Visual Basic functions returns variables' data types, but not the exact type. They return a True/False value indicating whether a variable holds a numeric value, a date or an array. The following functions are used to validate user … [Read more...] about IsNumeric(), Isdate() and IsArray() Functions
Scope of a Variable
In addition to its type, a variable also has a scope. The scope (or visibility) of a variable is the section of the application that can see and manipulate the variable. If a variable is declared within a procedure, only the code in the specific … [Read more...] about Scope of a Variable
Lifetime of a Variable
In addition to type and scope, variables have a lifetime, which is the period for which they retain their value. Variables declared as Public exist for the lifetime of the application. Local variables, declared within procedures with the Dim or … [Read more...] about Lifetime of a Variable
Constants in Visual Basic
Some variables don't change value during the execution of a program. These variables are constants that appear many times in your code. For instance, if your program does math calculations, the value of pi (3.14159. . .) might appear many times. … [Read more...] about Constants in Visual Basic