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
2010-10-08 08:40:25
Really? 123$ is the same as "123"? Wow - I never knew that!
teedyay
2010-10-08 08:45:10
This is true only for variable names, not for constant literals!
Yossarian
2010-10-08 08:54:44
Yeah, I'd seen them in that context, but that didn't seem to be what the question was about...
teedyay
2010-10-08 09:02:12
@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
2010-10-08 09:42:08
According the article: "Literals can also use the identifier type characters as can variables, constants, and expressions." Can you elaborate?
Michael Baker
2010-10-08 09:54:33
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
2010-10-08 10:01:12
@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
2010-10-08 10:26:35
MarkJ
2010-10-08 11:32:23
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
2010-10-08 17:33:16
+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
2010-10-08 17:11:44