14

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)

3 Answers 3

23

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).

2
  • 1
    Great answer! Knowing this I was finally able to search more information about it. "The last character of the name can be a dollar sign ($) to indicate a string variable or a percent sign (%) to indicate an integer variable." Interesting, but I agree that it's a bit ambiguous in VB.NET. May 9, 2013 at 5:07
  • @RowanFreeman I agree. It's an interesting historical artefact, but in VB.NET it's plain confusing to people like me who weren't around back then. May 9, 2013 at 5:11
17

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
5

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.