We help IT Professionals succeed at work.

Variable% and Variable$, what are these?

samsonite1023
on
Medium Priority
1,060 Views
Last Modified: 2010-05-02
Why do I often see a $ or % after a variable when dimensioning it?  Someone please let me know why one would use these, and if there are any others besides % and $ and what they do.

Thanks!

-Sam
Comment
Watch Question

Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016

Commented:
This was the old way of declaring string ($) and long (%) variables.
CERTIFIED EXPERT
Top Expert 2012
Commented:
The short answer:
Dim variable$  
is exactly the same as
Dim Variable As String
and
Dim Variable%
as
Dim Variable As Integer

It is legacy syntax that dates from the pre-VB days.
% = Integer
@ = Currency
$ = String
! = Single
# = Double
& = Long

Try and avoid using it, it is obsolete and no longer supported in VB.NET

Anthony
Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016

Commented:
Anthony is correct. % is an integer.

Commented:
type-declaration character
A character appended to a variable name indicating the variable's data type.

2 statements below are the same.  If you leave out the $ in the second one, it would be declared as a variant.
dim strName as string
or
dim strName$

Here's a list of the ones I can remember...
@ = currency
% = integer
& = long
! = single
# = double
$ = string

Author

Commented:
What about #if?

I've just recently seen that, what does it mean?

Thanks a lot for your posts!
Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016

Commented:
This is for conditional compilation. Just copied from the help file:

Conditionally compiles selected blocks of Visual Basic code.

Syntax

#If expression Then

statements

[#ElseIf expression-n Then

[elseifstatements]]

[#Else

[elsestatements]]

#End If

The #If...Then...#Else directive syntax has these parts:

Part Description
expression Required. Anyexpression, consisting exclusively of one or moreconditional compiler constants, literals, and operators, that evaluates to True or False.
statements Required. Visual Basic program lines or compiler directives that are evaluated if the associated expression is True.
expression-n Optional. Any expression, consisting exclusively of one or more conditional compiler constants, literals, and operators, that evaluates to True or False.
elseifstatements Optional. One or more program lines or compiler directives that are evaluated if expression-n is True.
elsestatements Optional. One or more program lines or compiler directives that are evaluated if no previous expression or expression-n is True.


Remarks

The behavior of the #If...Then...#Else directive is the same as the If...Then...Else statement, except that there is no single-line form of the #If, #Else, #ElseIf, and #End If directives; that is, no other code can appear on the same line as any of the directives. Conditional compilation is typically used to compile the same program for different platforms. It is also used to prevent debugging code from appearing in an executable file. Code excluded during conditional compilation is completely omitted from the final executable file, so it has no size or performance effect.

Regardless of the outcome of any evaluation, all expressions are evaluated. Therefore, allconstants used in expressions must be defined ? any undefined constant evaluates asEmpty.

Note   The Option Compare statement does not affect expressions in #If and #ElseIf statements. Expressions in a conditional-compiler directive are always evaluated with Option Compare Text.

Author

Commented:
Thanks

-Sam