Visual Basic, like all object-oriented programming languages. has many different variable types. To declare a variable in VB, we can simply write:

Dim VariableName as VariableType

For example, a string named strName could be declared by writing:

Dim strName as String

To concatenate, or combine, two strings together we use the “&” symbol. For example:

Dim strFullName = strFirst & ” ” & strSecond

NOTE: We can also use the “+” symbol for concatenation. However, this is not used as often in Visual Basic because it is not as straightforward.

If a line of code gets too long, you can extend it to the next line by using an underscore. For example:

strString = strFirst & strSecond & _
strThird

Visual Basic has many types of variables. The most common are:

  • Byte - An integer from 0 to 255
  • Short - An integer from -32,768 to 32,767
  • Integer - An integer from -2,147,483,648 to 2,147,483,647
  • Long - An integer from about -9.2 X 1018 to 9.2 X 1018
  • Single - A single precision floating point number from approximately -3.4 X 1038 to 3.4 X 1038 (for big numbers), or from -1.5 X 10-45 to 1.5 X 10-45 (for small, fractional numbers)
  • Double - A double-precision floating point number from approximately -1.8 X 10308 to 1.8 X 10308 (for big numbers), or from -5.0 X 10-324 to 5.0 X 10-324 (for small fractional numbers)
  • Decimal - A 128-bit fixed-point fractional number that supports up to 28 significant digits
  • Char - A single 16-bit unicode character
  • String - A variable-length series of unicode characters
  • Boolean - A true or false value
  • Date - Represents any date and time from 12:00:00 AM, January 1 of the year 1 in theGregorian calendar, to 11:59:59 PM, December 31 of the year 9999. Time values can resolve values to 100 nanosecond increments. Internally, this datatype is stored as a 64-bit integer.
  • TimeSpan - Represents a period of time, such as 10 seconds or 3 days. The smallest possible interval is 1 tick (100 nanoseconds)
  • Object - The ultimate base class of all object types. This can hold any datatype or object.

In Visual Basic, all variables initialize by default. For example:

Dim intNumber as Integer
Dim strString as String

Once this code runs, intNumber will instantiate to 0, and strString will instantiate to an empty string, or “”. Also, you can declare some variables without a datatype is obvious. For example:

Dim strayCat = “Look, a stray cat!”

Because Visual Basic understands what type of variable you were trying to create, it will automatically declare this as a string.

NOTE: You must be careful using this enhancement. For example:

Dim numberOne = 10
Dim numberTwo = 10.1

numberOne will be declared as an integer, while numberTwo will be declared as a double. Because the two numbers are declared as different variables, your code may not work.

Previous Next

Startup Discount
Small Business?
Contact us for an Enormous Discount!