33

For example:

variable1=Dir$(some_path)

vs.

variable1=Dir(some_path)

What is the difference?

Why not just do:

variable1=string(Dir(some_path))
TylerH
  • 20,799
  • 66
  • 75
  • 101
Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062

5 Answers5

54

Here is a Cheat Sheet for DataTypes

Variable End with:

$ : String
% : Integer (Int16)
& : Long (Int32)
! : Single
# : Double
@ : Decimal

Start with:

&H : Hex
&O : Octal

Comparison between VB and VB.Net (reference)

Visual Studio .Net added Literal Types (reference)

Value End with: (For more complete list, refer the the reference)

S : Short (Int16)
I : Integer (Int32)
L : Long (Int64)
F : Single
R : Double
D : Decimal
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
Gerhard Powell
  • 5,965
  • 5
  • 48
  • 59
16

I think that the $ version returns a String, and the non $ version returns a variant.

Mid vs Mid$

http://forums.devarticles.com/microsoft-access-development-49/mid-function-vs-mid-26315.html

Community
  • 1
  • 1
Jason Evans
  • 28,906
  • 14
  • 90
  • 154
  • why not just do: variable1=string(Dir(some_path)) – Alex Gordon Aug 02 '10 at 16:11
  • ***Let's find out:*** `Debug.Print TypeName(Mid("abc", 2, 1))` returns `String`, *and* `Debug.Print TypeName(Mid$("abc", 2, 1))` **also** returns `String`. On the other hand, there's [**this**](https://i.imgur.com/5KroAb8.png). – ashleedawg Nov 28 '18 at 18:02
9

The dollar sign indicates a string will be returned instead of a variant.

LittleBobbyTables - Au Revoir
  • 32,008
  • 25
  • 109
  • 114
3

Dir() returns the result as the variant data type. Dir$() returns the result as the string data type.

Dylan West
  • 627
  • 10
  • 22
0

some uses $ version for its purported efficiency(as it accepts and outputs statically-typed variables only). I don't know how much is the speed difference between statically-typed and variant type, just benchmark

Michael Buen
  • 38,643
  • 9
  • 94
  • 118
  • So why not test it rather than post an answer that is ambiguous as to whether you believe there is a benefit (purportedly) but then makes clear you don't know? :) – brettdj Dec 01 '13 at 00:35
  • @brettdj I know $ version functions are faster than non-dollar(accepts variant parameter), but didn't know the speed in speed could be staggering for *millions* of iterations. Never I done something in a loop, just letting the database do the heavy lifting, perhaps if I'm a component maker I can appreciate the efficiency of $%&!#@ (I'm not cursing eh lol) version functions. For some, it is a purported efficiency; but for some, the efficiency is real and tangible. Here's a sample benchmark http://www.shamrock-software.eu/vb.htm – Michael Buen Dec 01 '13 at 01:13
  • see http://stackoverflow.com/questions/7982220/differentiate-trim-and-trim-in-vba/7988125#7988125 – brettdj Dec 01 '13 at 04:17
  • @MichaelBuen "i'm not cursing" :D – johny why May 07 '21 at 16:39