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. These options… [Continue Reading]
Using Variables and Data Types
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 starting with VB 2008, you should use strictly typed variables. However,… [Continue Reading]
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 variables are objects. That’s because a variable that holds dates is… [Continue Reading]
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 data-conversion functions of VB (CInt() to convert a numeric value to an Integer, CDbl()… [Continue Reading]
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 formats numbers and dates in many ways… [Continue Reading]
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 check: the check’s number, amount, date, and so on. All these pieces of information… [Continue Reading]
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 input, as well as data stored in files, before you process them. IsNumeric() Returns… [Continue Reading]
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 procedure has access to that variable; this variable doesn’t exist… [Continue Reading]
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 Private statement, live as long as the procedure. When the procedure finishes, the local variables… [Continue Reading]
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. Instead of typing the value 3.14159 over and over again, you can define a… [Continue Reading]