Visual Basic/Variables and Types

This lesson will instruct you on the creation of variables in Visual Basic 6, as well as variable types. This lesson requires a very basic understanding of the interface in Visual Basic 6. If you have not done so already, it is recommended that you read the Introduction to VB6.

Setting Up The Project
The code that you type between these two lines will be executed when your program launches. Because of that, its a handy place to try out the new concepts like the ones you'll be learning in this article.
 * 1) Create a Standard EXE in the same way that you did in the previous lesson.
 * 2) Double-click anywhere in the blank content of your form. This will bring up the code editor with the following code:

Object Types and Naming Scheme
These are very important things to remember in order to make your source code more easily intelligible.

File Types
You will use various file types while using VB6. These are some the most common ones.
 * File type: What people call it
 * Prefix: What people prefix the filename with
 * Extension: What file extension is used
 * Description: What its used for most often

Examples of common file names:
 * OddCalc.vbp
 * frmMain.frm
 * frmAbout.frm
 * frmPrintInvoice.frm
 * modMain.bas
 * modSettings.bas
 * modDeclares.bas
 * modWinsock.bas
 * clsWinsock.cls
 * ucCustomButton.ctl
 * ucTreeView.ctl
 * ucWinsock.ctl

Variables
A variable is a word or letter used to reference data used in a program. That data may be an integer like "23", a floating point number like "23.23", a string like "Hello World!", and many other data type.

Variable Names
The following are the requirements when naming the variables in Visual Basic:
 * It must be less than 255 characters
 * No spacing is allowed
 * It must not begin with a number
 * Period (dot) is not permitted

For the sake of making sure other people can look at your code and know what you were thinking:
 * Prefix your variable name with the appropriate prefix for your variable's data type.
 * Make sure the body of your variable name makes it easy to tell what its used for.
 * Don't use an ambiguous name like "intUhhhh" or "strX" unless its use is within a very small scope of the program, and even then try not to do it much. Try using descriptive names. For example, to save the total marks sustained by a particular person in an integer, use something like int Marks/Marks Sustained rather than a random string of characters.

Using variables
In order to use a variable it must first be declared, then its usually modified to represent some sort of value, then its often outputted in some way so that you can see a result from it.

Example variable declaration, Setting, and Output: Example variable declaration, Setting, Modification, and Output: Now, go put those lines between Private Sub Form_Load and End Sub in your program to execute those instructions when the program starts.

Variable Declaration Scope
The keyword used to declare a variable and the location in which its declared defines the scope and duration in which a variable is available.

Keyword: Declaration location: Scope: Duration:
 * The first word used in a variable declaration
 * "Procedure" means the variable is declared within a Sub, Function, or Property
 * "Module" means the variable is declared near the top of the module, not inside a procedure
 * "Procedure" means the variable is available only within the Sub, Function, or Property in which it was defined
 * "Module" means the variable is available only within the module in which it was defined
 * "Project" means the variable is available throughout all modules in the project
 * "Procedure" means the variable's value is gone once the procedure its available in has finished
 * "Application" means the variable's value will remain throughout running of the program

Data Types
Data Types are classifications of what sort of data is contained inside of a variable. Variables of certain data types may only contain values which are acceptable for that data type. The following tables describe the data types available in VB6. You may select any of the words under the column "Type" in order to use that data type.

Examples:

Control Types
Examples of common object names:
 * txtName
 * txtAddress
 * cboYear
 * cmdOK
 * cmdCancel

Visual Basic/Les types en VB