Variables in Visual Basic.NET

 

Contents

Contents

Variables

In programing a variable is simply a place to store data. A variable has a name and a data type. In Visual Basic .NET, a variable is declared using the Dim (short for Dimension) statement. Here is the syntax:

Dim varName As varType
                                          

varName is the name of your Variable. varType is the data type of the variable. Types include string, integer, double, boolean, etc.

For example, to declare an integer named MyInt use:

Dim MyInt As Integer
                                          

Data Types

Data Types define the type of data that a variable can store. Some variables store numbers, others store names. The basic types that can be used in Visual Basic .NET are:

SByte

Meaning signed byte (added in VB.NET 2005). 8 bits, stores integer values from -128 to 127.

Dim sbytMyVariable As SByte
                                          

Byte

8 bits, stores integer values from 0 to 255.

Dim bytMyVariable As Byte
                                          

Short

Meaning short integer. 16 bits (2 bytes), stores integer values from -32,768 to 32,767.

Dim shrtMyVariable As Short
                                          

UShort

Meaning unsigned short integer (added in VB.NET 2005). 16 bits (2 bytes), stores integer values from 0 to 65,535.

Dim ushrtMyVariable As UShort
                                          

Integer

32 bits (4 bytes), stores integer values from -2,147,483,648 to 2,147,483,647.

Dim intMyVariable As Integer
                                          

UInteger

Meaning unsigned integer (added in VB.NET 2005). 32 bits (4 bytes), stores integer values from 0 to 4,294,967,295.

Dim uintMyVariable As UInteger
                                          

Long

Meaning long integer. 64 bits (8 bytes), stores integer values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

Dim lngMyVariable As Long
                                          

ULong

Meaning unsigned long integer (added in VB.NET 2005). 64 bits (8 bytes), stores integer values from 0 to 18,446,744,073,709,551,615.

Dim ulngMyVariable As ULong
                                          

Single

Meaning single-precision floating point. 32 bits (4 bytes), stores floating-point values from -3.40282347e38 to 3.40282347e38. Cannot store an exact zero due to the required normalized form. Smallest positive value greater than zero is 1.401298e-45. Biggest negative value less than zero is -1.401298e-45.

Dim sglMyVariable As Single
                                          

Double

Meaning double-precision floating point. 64 bits (8 bytes), stores floating-point values from -1.7976931348623157e308 to 1.7976931348623157e308. Cannot store an exact zero due to the required normalized form. Smallest positive value greater than zero is 4.94065645841247e-324. Biggest negative value less than zero is -4.94065645841247e-324.

Dim dblMyVariable As Double
                                          

Decimal

128 bits (16 bytes), stores floating-point values from -79,228,162,514,264,337,593,543,950,335 to 79,228,162,514,264,337,593,543,950,335. Stores an exact zero.

Dim decMyVariable As Decimal
                                          

Boolean

Requires 1 bit (1 byte used), stores boolean true/false values. For a false value, one may enter "false" or "0" in the source code; for a true value, enter "true" or "-1".

Dim boolMyVariable As Boolean
                                          

Date

64 bits (8 bytes), stores a date and time in the range of January 1, 0001 12:00:00 AM to December 31, 9999 11:59:59 PM.

Dim dtMyVariable As Date
                                          

Char

16 bits (2 bytes), stores one unicode symbol in the range of 0 to 65,535.

Dim chMyVariable As Char
                                          

String

10 bytes + text length (maximum total: 2GB), stores any series of characters. e.g. word, phrase, sentence, paragraph, etc.

Dim strMyVariable As String
                                          

Object

32 bits (4 bytes), stores a reference to a Visual Basic object.

Dim objMyObject As Object
                                          

Using Variables

Assigning Values

A value is the data contained in a variable. To assign a value to a variable that is already declared, use an equal sign.

Suffix for Literals

Integral literals, such as 42 and 1000, are of type Integer by default. String and character literals, such as "Hello World" and "A", are of type String by default. To specify the type for a literal, suffixes are used. The suffixes are appended immediately after the literals, in the manner <literal><suffix>, without any whitespace between.

Examples

For string and char variables, use double quotes around the value:

strMyVariable = "The String"
chrMyVariable = "À"c
                                          

For date variables, use hashes/pounds around the value, in the format #<month>/<day>/<year> <hour>:<minute>:<second> <AM|PM>#:

dtMyVariable = #7/4/1776 12:01:50 PM#
                                          

For all others, remove the quotes and hashes/pounds:

bytMyVariable = 1
sbytMyVariable = -2
shrtMyVariable = 10S
ushrtMyVariable = 10US
intMyVariable = 100
uintMyVariable = 100UI
lngMyVariable = 1000L
ulngMyVariable = 1000UL
sngMyVariable = 1.234F
dblMyVariable = 1.567R
decMyVariable = 1234567.89D
boolMyVariable = True
objMyObject = New Object
                                          

To assign a variable the value of another variable, simply replace the value on the right side of the equal sign with the name of the variable that holds the desired data.

You can also assign a value to a variable in the declaration itself.

Dim myVariable As String = "StringValue"
                                          

Important: Visual Basic always assigns the value of the right variable to the left variable. The variable on the left takes the value of the right variable. The variable on the right does not change.

Constants

Constants are like variables that don't change. They take the place of values that you would not like to type over and over. Constants are declared using the keyword "Const". Their values are defined in their declaration - they also use data types. Here is the syntax:

Const cnstMyConstant As String = "The very long string"
                                          

Here is an example:

Const cnstPi As Single = 3.14159265F
                                          

 

 

All text is available under the terms of the GNU Free Documentation License
Source : Wikibooks