Introduction
This article is about how to declare variables in VB.NET without using the As
type.
Type Specifying Characters
A type character following a non-escaped identifier (with no whitespace between them) denotes the type of the identifier.
You can use it in place of the As
type portion of declaration, for example the following declaration:
Dim str$
Dim str As String
Type character also detracts from readability. If you use them be sure that if the declaration includes a type character, the character
agrees with the type specified in the declaration itself; otherwise, a compiler time error occurs
if your declaration omits the type.
The complier will insert the type character implicitly substituted as the type of the declaration.
The following syntax lists the type characters available in VB.NET (there is no type character for Byte
or Short
).
Type Character |
Character |
Integer |
% |
Long |
& |
Decimal |
@ |
Single |
! |
Double |
# |
String |
$ |
Example of declaration
Dim num%
While declared variable we can also initialize a value to it.
num=500
Also we can declare multiple variables at a time.
Dim num, n%
We can also initialize it at the time of declaration.
Dim num = 250, n = 670,val%
Different types can be declare at a time.
Dim number = 180%, str$, salary@
All types of declaration can be viewed as follows.
Dim number% Dim num& Dim salary@ Dim amount! Dim rate# Dim name$
Note
No spaces are allowed within a variable name and type character. Syntax: Dim [variableName] space [Type Character]
.