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

  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

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
File Type Prefix Extension Description
ProjectvbpGeneral project options
FormfrmfrmGUI information and private code
BAS ModulemodbasProject-wide accessible functions
Class ModuleclsclsProject-wide accessible subroutines
User ControlucctlControl object (Like an OCX with source code)
Property pagepagpagProperty information
OLE ControlocxCompiled control object
Dynamic Link LibdllSubs 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

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:

 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

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
DimProcedureProcedureProcedure
StaticProcedureProcedureApplication
PrivateModuleModuleApplication
PublicModuleProjectApplication
ConstProcedureProcedureProcedure
Private ConstModuleModuleApplication
Public ConstModuleProjectApplication
GlobalModuleProjectApplication

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:

 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
Type Size Range of Values Prefix Example Variable Name
Byte1 byte0 to 255bytbytFirstChar
Integer2 bytes-32,768 to 32,767intintCount
Long4 bytes-2,147,483,648 to 2,147,483,648lnglngHwnd
Single4 bytesNegative values: -3.402823E+38 to -1.401298E-45
Positive values: 3.402823E+38 to 1.401298E-45
sngsngPi
Double8 bytesNegative values: -1.79769313486232e+308 to -4.94065645841247E-324
Positive values: 1.79769313486232e+308 to 4.94065645841247E-324
dbldblAngle
Currency8 bytes-922,337,203,685,477.5808 to 922,337,203,685,477.5807curcurTotalCost
Non-numeric Data Types
Type Size Range of Values Prefix Example Variable Name
StringLength of string1 to 65,400 characters (fixed length)strstrName
StringLength + 10 bytes0 to 2 billion characters (variable length)strstrHTM4
Date8 bytesJanuary 1, 100 to December 31, 9999dtmdtmBirth
Boolean2 bytesTrue or FalseblnblnToggle
Object4 bytesAny embedded objectobjobjCurrent
Variant16 bytesAny value as large as Double (numeric)vntvntNumber

Control Types

Control Type Prefix
TextBoxtxt
PictureBoxpic
Modulemod
Formfrm
Labellbl
Framefra
CommandButtoncmd
CheckBoxchk
RadioButtonrad
ComboBoxcbo
ListBoxlst
Scroll Barsbr (no orientation needed)
Timertmr
DriveListBoxdrv
DirListBoxdir
FileListBoxfil
Shapeshp
Imageimg
Datadat
OLEole
ListViewlvw
TreeViewtvw

Examples of common object names:

  • txtName
  • txtAddress
  • cboYear
  • cmdOK
  • cmdCancel




Learning Visual Basic
Previous: Visual Basic/Introduction Next: Visual Basic/Control Structures and Logical Expressions
Category:Visual Basic Category:Computer programming data types
Category:Computer programming data types Category:Visual Basic