tags:

views:

90

answers:

2

I have recently come across numeric literals such as 10! and 50# in Visual Basic programs. Could anyone tell me what these punctuation marks mean?

+7  A: 

They are called type declaration characters. This article has more information.

  % Integer
  & Long
  ! Single
  # Double
  $ String
  @ Currency
Michael Baker
Really? 123$ is the same as "123"? Wow - I never knew that!
teedyay
This is true only for variable names, not for constant literals!
Yossarian
Yeah, I'd seen them in that context, but that didn't seem to be what the question was about...
teedyay
@Michael Baker> you misunderstood the article. They AREN'T attached to literal, but to the variable name, and is used like `Dim x as String` => `Dim x$`
Yossarian
According the article: "Literals can also use the identifier type characters as can variables, constants, and expressions." Can you elaborate?
Michael Baker
Has the hyperlink been edited? Within seconds I opened the same link twice and end up at two different articles! This one (http://support.microsoft.com/kb/191713) relates to VB6 and doesn't mention literals. This one (http://msdn.microsoft.com/en-us/library/s9cz43ek(v=VS.71).aspx) talks about literals but relates to VB.NET.
onedaywhen
@Michael Baker: I rolled back. The article about literals only applies to VB.NET and the question has only ever had the VB6 tag.
onedaywhen
MarkJ
Normally, you'd see these attached to (numeric) literals in one of two places: is Const statements (since compiler constants are just replacement text, they can't have a "real" type); or when coercing a literal to Long, Double or Currency to avoid overflow (although I always preferred an explicit Ctype for maintainability).
Stan Rogers
+5  A: 

Using these characters specifies the data type of a numeric literal.

I thought this would be covered in the VB6 manual online but I can't find it.

However I just proved it with the TypeName function in the VB6 IDE Immediate Window:

? typename(10!)
Single
?typename(10#)
Double
?typename(10%)
Integer
?typename(10&)
Long
?typename(10@)
Currency

PS Be aware that a VB6 Integer is 2 bytes, -32,768 to 32,767.

MarkJ
That's a good idea, MarkJ. I wish I'd thought of it.
Brian Hooper