Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to declare a variable in VB.NET without using the As type

0.00/5 (No votes)
17 Aug 2012 1  
How to declare variables in VB.NET without using the As type.

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$ 

'str is a variable name and $ indicates as String Type.
'the above declaration is as same thing as following.
  
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%       'num is variable of Integer type declared.

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%        'num & n is variable of Integer type declared.

We can also initialize it at the time of declaration.

Dim num = 250, n = 670,val%      'num,n & val is variable of Integer type.

Different types can be declare at a time.

Dim number = 180%, str$, salary@
'number is variable of Integer Type & initialized,
'str is variable of String Type, Salary is variable of Decimal Type

All types of declaration can be viewed as follows.

Dim number%    'number is variable of Integer Type.
Dim num&       'num is variable of Long Integer Type.
Dim salary@    'salary is variable of Decimal Type.
Dim amount!    'amount is variable of Single Type.
Dim rate#      'rate is variable of Double Type.
Dim name$      'name is variable of String Type.

Note

No spaces are allowed within a variable name and type character. Syntax: Dim [variableName] space [Type Character].

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here