Comments are just as important in Visual BASIC as they are in any other language. Without them, code that you wrote when you were up until 5AM may not make any sense. With the comments you can leave notes as to why you put a line or block of code there. Comments are always ignored by the compiler and will not affect your program in any way. To leave comment in Visual BASIC we use the apostrophe (').

' This is a comment


In Visual BASIC before you use a variable, you must declare it. To declare a variable you must first decide of which type it will be. Like the components, every variable type has a prefix that you should add to each variable. Here is a list of VIsual BASIC variable types and their prefixes:
Name Description Prefix Range
BooleanTrue or Falsebln true or false
ByteSingle byte of databyt 0 to 255
Char Characterchar 0 to 65535
Currency Moneycur -922 337 203 685 477.5808 to 922 337 203 685 477.5807
Date Date/Timedtm 0:00:00 (midnight) on January 1, 0001 through 11:59:59 PM on December 31, 9999
Decimal Decimal Datadec 0 through 79 228 162 514 264 337 593 543 950 335
Double Decimaldbl 4.94065645841247E-324 to 1.79769313486232E308
Integer Whole numberint -32,768 to 32,767
Long Whole Numberlng -2 147 483 648 to 2 147 483 647
Object When declaring something as a componentSame as component Object
Single Decimalsng 1.401298E-45 to 3.402823E38
String What's in the quotation marksstr Any writable character

The Char datatype is actually an integer because the number coincides with a character. For example 65 is A, 66 is B, and so on. Object is just a place holder for the components included (and ones you make yourself). You can have a segment of code that creates a new object or component. We will cover this more in later areas as it deals with the objectpart of Visual BASIC.

To declare a variable, we use the Dim statement. What the Dim statement does is tells the compiler that what comes next will be variables. After the word Dim, we put the name of a variable. There are some rules other than using the prefixes that we follow when naming variables, they cannot be more than 255 characters long, must begin with a letter, and should be meaningful. You should also keep in mind that variables can only hold one value at a time. This doesn't mean we can't use x as a counter, it is common to use a one letter variables for counters. After the variable name, we put the word as and then the variable type. Here are some examples of variable declaration:

Dim charLetterGrade As Char
Dim decGrade As Decimal
Dim blnChoice As Boolean

If you do not declare a variable before you use it, you will get an error. Unlike other languages, Visual BASIC automatically sets your variables to 0 so there's no need to initialize them.


Say you wanted to write a program that had the salestax variable to 6%. You wouldn't want that to ever change, so you could make that a constant. A constant works the same way as a variable, but it uses the keyword Const before the data type, instead of Dim and must be set in the same line of code. The same rules apply to constants as they do with normal variables, but with constants it is a good idea to capitalize them.

const DBLSALESTAX as decimal = .06


In order to make variables appealing, we need to be able to do mathematic operations in them. In programming the computer does mathematical operations in the order of operations (PEMDAS).
ModModulus Division
^To the power of
+=Variable is equal to itself plus another
-=Variable is equal to itself minues another
*=Variable is equal to itself times another
/=Variable is equal to itself divided by another

Modulus division is taking a number dividing it by the number after the modulus sign (Mod) and then taking the remainder of that would be modulus. In other words 56 % 10 would be 6 because 56 divided by 10 if 5 remainder 6. You'll see more of this in later sections dealing with functions.

It's easy to use these other operators in a variable:

const DBLSALESTAX as Double = .06     'defines a constant and sets salestax to .06
Dim intAge as integer = 1 'Sets dblAge to 1
charGrade = "D" 'Sets charGrade to D
strName = "Sam" 'Sets strName to Sam
decNum = txtNum.Text 'Sets decNum to what a user enters in the TextBox
intAge += 5 'Makes intAge equal to itself plus 5

By now you should have an understanding of how variables work. Remember that variables declared in a submodule are native to that submodule and therefore cannot be used in other modules. Characters are enclosed in double quotes ("), not single quotes. If you don't have a good idea on how they work then reread the tutorial. In the next section we will be getting input from the user and printing out that data.

Next Section: Input, Output, and Closing

Link Directory | link directory | GeekySpeaky: Submit Your Site! | Blog Matter

Copyright © 2004-2017 Sean Noble, All rights reserved
Read our Disclosure Policy