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

edit
  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:
Private Sub Form_Load()

End Sub

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.


Object Types and Naming Scheme

edit

These are very important things to remember in order to make your source code more easily intelligible.

File Types

edit

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
File Type Prefix Extension Description
Project vbp General project options
Form frm frm GUI information and private code
BAS Module mod bas Project-wide accessible functions
Class Module cls cls Project-wide accessible subroutines
User Control uc ctl Control object (Like an OCX with source code)
Property page pag pag Property information
OLE Control ocx Compiled control object
Dynamic Link Lib dll Subs and functions accessible by other programs

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

edit

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

edit

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

edit

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:

 Dim strName as String ' Declares a string called strName
 strName = "Billy Bob" ' Sets the value of strName
 MsgBox strName        ' Outputs the value of strName to a Message Box

Example variable declaration, Setting, Modification, and Output:

 Dim strName as String ' Declares a string called strName
 strName = "Billy Bob" ' Sets the value of strName
 strName = strName & " Thornton" ' Appends " Thornton" to strName
 MsgBox strName        ' Outputs the value of strName to a Message Box

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
edit

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:

  • The first word used in a variable declaration

Declaration location:

  • "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

Scope:

  • "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

Duration:

  • "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


Keyword Declaration location Scope Duration
Dim Procedure Procedure Procedure
Static Procedure Procedure Application
Private Module Module Application
Public Module Project Application
Const Procedure Procedure Procedure
Private Const Module Module Application
Public Const Module Project Application
Global Module Project Application

Data Types

edit

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:

 Dim intCount As Integer
 Dim lngHwnd As Long
 Dim sngPi as Single
 Dim strName As String
 Dim dtmBirth As Date
 Dim blnToggle As Boolean
Numeric Data Types
edit
Type Size Range of Values Prefix Example Variable Name
Byte 1 byte 0 to 255 byt bytFirstChar
Integer 2 bytes -32,768 to 32,767 int intCount
Long 4 bytes -2,147,483,648 to 2,147,483,648 lng lngHwnd
Single 4 bytes Negative values: -3.402823E+38 to -1.401298E-45
Positive values: 3.402823E+38 to 1.401298E-45
sng sngPi
Double 8 bytes Negative values: -1.79769313486232e+308 to -4.94065645841247E-324
Positive values: 1.79769313486232e+308 to 4.94065645841247E-324
dbl dblAngle
Currency 8 bytes -922,337,203,685,477.5808 to 922,337,203,685,477.5807 cur curTotalCost
Non-numeric Data Types
edit
Type Size Range of Values Prefix Example Variable Name
String Length of string 1 to 65,400 characters (fixed length) str strName
String Length + 10 bytes 0 to 2 billion characters (variable length) str strHTM4
Date 8 bytes January 1, 100 to December 31, 9999 dtm dtmBirth
Boolean 2 bytes True or False bln blnToggle
Object 4 bytes Any embedded object obj objCurrent
Variant 16 bytes Any value as large as Double (numeric) vnt vntNumber

Control Types

edit
Control Type Prefix
TextBox txt
PictureBox pic
Module mod
Form frm
Label lbl
Frame fra
CommandButton cmd
CheckBox chk
RadioButton rad
ComboBox cbo
ListBox lst
Scroll Bar sbr (no orientation needed)
Timer tmr
DriveListBox drv
DirListBox dir
FileListBox fil
Shape shp
Image img
Data dat
OLE ole
ListView lvw
TreeView tvw

Examples of common object names:

  • txtName
  • txtAddress
  • cboYear
  • cmdOK
  • cmdCancel




Learning Visual Basic
Previous: Visual Basic/IntroductionNext: Visual Basic/Control Structures and Logical Expressions