PDA

Click to See Complete Forum and Search --> : Classic VB - What Are #, $, etc.?


MartinLiss
Nov 25th, 2005, 07:56 PM
The symbols are "shortcuts" for several of the Visual Basic data types. # is the shortcut for Double, so

Dim MyVar#
is the same as
Dim MyVar As Double

Similarly:
! = Single
& = Long
@ =Currency
% = Integer
$ = String

Note: Using the shortcuts is not recommended since they take away from the readability and self-documenting features of Visual Basic.

penagate
Nov 25th, 2005, 11:56 PM
In addition, the type symbols can be used to differentiate between functions of the same name but different return types. For example, the VB runtime function Trim(), which removes whitespace from the left and right of a string, returns a Variant containing a String, but the function Trim$() returns a String directly. Since you should always try not to use Variants, as they are large and slow, using the Trim$() function will be more efficient than Trim() as there is no need to convert to and from the Variant type.

Other similar functions are Left()/Left$(), Right()/Right$(), Mid()/Mid$(), and many other string functions.

Note that you cannot create two functions with the same name but different types, even though there are examples of it in the VB runtime library.

RhinoBull
Nov 26th, 2005, 08:29 PM
In addition for those that wonder how to identify via code variable's type (especially those that were declared with type shortcuts) here is a sample:

Private Sub Command1_Click()
Dim var1%, var2&, var3#, var4!, var5@, var6$

Debug.Print "var1: " & TypeName(var1)
Debug.Print "var2: " & TypeName(var2)
Debug.Print "var3: " & TypeName(var3)
Debug.Print "var4: " & TypeName(var4)
Debug.Print "var5: " & TypeName(var5)
Debug.Print "var6: " & TypeName(var6)

End Sub