Rowan Freeman
Rowan Freeman

Reputation: 16348

What does a percentage symbol mean as part of a variable name?

I'm just looking at some VB.NET code and I came across this:

Dim var%

Later var is set to 0.

What's the purpose of the percent sign (%)?

(Google and SO search failed me)

Upvotes: 14

Views: 14841

Answers (3)

Idle_Mind
Idle_Mind

Reputation: 39122

It is shorthand for declaring "var" as of Type Integer, and has roots in the early, early days of BASIC (yes...old-school DOS BASIC).

So this:

Dim var%

is equivalent to:

Dim var As Integer

Here is the actual MS documentation: https://support.microsoft.com/en-us/kb/191713

      %                 Integer
      &                 Long
      !                 Single
      #                 Double
      $                 String
      @                 Currency

Upvotes: 18

Gary
Gary

Reputation: 1895

Putting a % sign at the end of the variable name in Visual Basic indicates that it is an integer. This was used by programmers in the old VB days, I am not sure why it is still present in VB.NET. Don't use it in new code, for all you know it might be gone in future versions of VB.NET.

& : Long

% : Integer

'# : Double

! : Single

@ : Decimal

$ : String

Upvotes: 5

Dim varname% is ancient BASIC syntax for "varname is an integer". This has been around for a very long time in the history of the BASIC family, and is supported in Visual Basic.NET (although I, personally, wouldn't recommend it - it can be rather opaque, as discovered by you).

Upvotes: 23

Related Questions