Variables in Visual Basic are declared using the Dim
keyword. For example, this declares a new variable called counter
with the data type Integer
:
Dim counter As Integer
A variable declaration can also include an access modifier, such as Public
, Protected
, Friend
, or Private
. This works in conjunction with the variable's scope to determine its accessibility.
Access Modifier | Meaning |
---|---|
Public | All types which can access the enclosing type |
Protected | Only the enclosing class and those that inherit from it |
Friend | All types in the same assembly that can access the enclosing type |
Protected Friend | The enclosing class and its inheritors, or the types in the same assembly that can access the enclosing class |
Private | Only the enclosing type |
Static | Only on local variables and only initializes once. |
As a shorthand, the Dim
keyword can be replaced with the access modifier in the variable's declaration:
Public TotalItems As Integer
Private counter As Integer
The supported data types are outlined in the table below:
Type | Alias | Memory allocation | Example |
---|---|---|---|
SByte | N/A | 1 byte | Dim example As SByte = 10 |
Int16 | Short | 2 bytes | Dim example As Short = 10 |
Int32 | Integer | 4 bytes | Dim example As Integer = 10 |
Int64 | Long | 8 bytes | Dim example As Long = 10 |
Single | N/A | 4 bytes | Dim example As Single = 10.95 |
Double | N/A | 8 bytes | Dim example As Double = 10.95 |
Decimal | N/A | 16 bytes | Dim example As Decimal = 10.95 |
Boolean | N/A | Dictated by implementing platform | Dim example As Boolean = True |
Char | N/A | 2 Bytes | Dim example As Char = "A"C |
String | N/A | source | Dim example As String = "Stack Overflow" |
DateTime | Date | 8 Bytes | Dim example As Date = Date.Now |
Byte | N/A | 1 byte | Dim example As Byte = 10 |
UInt16 | UShort | 2 bytes | Dim example As UShort = 10 |
UInt32 | UInteger | 4 bytes | Dim example As UInteger = 10 |
UInt64 | ULong | 8 bytes | Dim example As ULong = 10 |
Object | N/A | 4 bytes 32 bit architecture, 8 bytes 64 bit architecture | Dim example As Object = Nothing |
There also exist data identifier and literal type characters usable in replacement for the textual type and or to force literal type:
Type (or Alias) | Identifier type character | Literal type character |
---|---|---|
Short | N/A | example = 10S |
Integer | Dim example% | example = 10% or example = 10I |
Long | Dim example& | example = 10& or example = 10L |
Single | Dim example! | example = 10! or example = 10F |
Double | Dim example# | example = 10# or example = 10R |
Decimal | Dim example@ | example = 10@ or example = 10D |
Char | N/A | example = "A"C |
String | Dim example$ | N/A |
UShort | N/A | example = 10US |
UInteger | N/A | example = 10UI |
ULong | N/A | example = 10UL |
The integral suffixes are also usable with hexadecimal (&H) or octal (&O) prefixes:
example = &H8000S
or example = &O77&
Date(Time) objects can also be defined using literal syntax:
Dim example As Date = #7/26/2016 12:8 PM#
Once a variable is declared it will exist within the Scope of the containing type, Sub
or Function
declared, as an example:
Public Function IncrementCounter() As Integer
Dim counter As Integer = 0
counter += 1
Return counter
End Function
The counter variable will only exist until the End Function
and then will be out of scope. If this counter variable is needed outside of the function you will have to define it at class/structure or module level.
Public Class ExampleClass
Private _counter As Integer
Public Function IncrementCounter() As Integer
_counter += 1
Return _counter
End Function
End Class
Alternatively, you can use the Static
(not to be confused with Shared
) modifier to allow a local variable to retain it's value between calls of its enclosing method:
Function IncrementCounter() As Integer
Static counter As Integer = 0
counter += 1
Return counter
End Function