Visual Basic

Return to Main Page
Table of Contents
Return to Top

Variables and How to Declare them

Variables are where you store data such as numbers and words.
You would typically use variables to manipulate the data, such as adding two numbers

Variable Types

There are several kinds of basic variables in Visual Basic. These are the most commonly used

Using Variables

Variables are useless unless you store data in them. Inputting a value into a variable is easy. For instance, to store the sum of 5 + 5 in a variable, you would first write
 Dim myCoolVar as Integer then write
 myCoolVar = 5 + 5 


String Concatenation

String concatenation is an important tool to make your application look good and have proper language
It can be used to combine pre-defined text with variables that you defined and expect to change.

For instance, "The final cost is: $" & finalCost would write The final cost is $4.45
assuming that the value of 4.45 was stored in finalCost.
In this example, the text in  red  is the predefined string whereas  finalCost  is the variable.
These are combined with the use of the  & to produce the text that appears when the program is run. This is useful because the value of finalCost may change depending on your program.


GUI Elements

Visual Basic provides a large library of Graphical User Interface tools such as buttons and textboxes to use in your program.
Each of these items has a set of matching attributes and values. For instance, a Textbox may have attribute name and associated value TextBox1 . This gives the textbox a name. It also includes an attribute named Text, which is what appears inside the textbox when the program runs.

The following is a list of commonly used attributes

AttributeValueDescription
NameTextBox1A name that each item has. This is how items are referred to.
TextHello worldThe text that appears in/on the item (does not include ListBox type).
BackColorGreenThe color which should be used as the item's background. This element also accepts hexadecimal color codes such as #FF8800 (for orange).
TabIndex4The order in which items are selected when pressing Tab on the keyboard. Each item should have a unique number. The first element to be selected should have TabIndex of 0
VisibleFalseDetermines whether an object should be shown or not. Default is True

Generally, you will want to give each element a name that signifies what it is and what it will be used for. For instance, a Button to perform a main function could be called buttonMain whereas a button for clearing old data could be called buttonClear. Return to Top