Connecting Tech Pros Worldwide Forums | Help | Site Map

What's Your Function Flavor?

ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,219
#1   Feb 25 '07
All built-in VBA Functions that deal with String handling/manipulation actually come in 2 'Flavors' such as:
Left/Left$, Right/Right$, Format/Format$, Mid/Mid$, etc.
This is a throw back to the early days of Basic, when before you could specify a Variable of a specific Data Type, programmers had to use type-specification characters rather than explicit Data Type Declarations. Some of these typical Declaration characters were '$' for String, '%' for Integer, '&' for Long Integer, '#' for Double, etc. A typical Declaration would look like:

Expand|Select|Wrap|Line Numbers
  1. Dim MyDouble#
  2. MyDouble# = 4 * Atn(1)      'Pi
  3. Debug.Print MyDouble 
This same system was applied to Function Names in that Functions ending with a '$' would return a String while its counterpart returned a Variant as in Right$ and Right. We no longer use the characters to indicate the Data Type but the Functions still remain. Left() returns a Variant and Left$() returns a String. The bottom line is that if you know you are placing the return value of a String Handling Function into a String, always use the '$' version of the Function. A simple test will demonstrate this point:

Expand|Select|Wrap|Line Numbers
  1. For i = 1 to 2000000000
  2.   strValue = Left$(strValue, 3)
  3. Next i
will take approximately 1/2 as much time as in the 2nd code snippet below because you are forcing VBA to do an extra data type conversion (Variant to String)

Expand|Select|Wrap|Line Numbers
  1. For i = 1 to 2000000000
  2.   strValue = Left(strValue, 3)
  3. Next i
In my own tests the String flavor of this particular Function ran 53% faster than the Variant flavor.



nico5038's Avatar
Moderator
 
Join Date: Nov 2006
Location: The Netherlands
Posts: 2,232
#2   Feb 25 '07

re: What's Your Function Flavor?


Interesting, but MS did announce several years ago that the "$" versions would be dropped....

Nic;o)
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,219
#3   Feb 26 '07

re: What's Your Function Flavor?


Quote:

Originally Posted by nico5038

Interesting, but MS did announce several years ago that the "$" versions would be dropped....

Nic;o)

Thanks Nico, I wasn't aware of that. All I know is that I still use the '$' versions to this very day. I guess they'll be dropped as soon as they do away with Macros (LOL).
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,730
#4   Feb 26 '07

re: What's Your Function Flavor?


Quote:

Originally Posted by nico5038

Interesting, but MS did announce several years ago that the "$" versions would be dropped....

Nic;o)

That's why I don't use them any more too.
MS are planning to drop them from the language. As you say though, it's a bit like some of their other plans like moving to ADODB from DAO etc.
I must admit that doing away with Macros won't upset me too much, but then I was warned early on that they were on their way out so I never got into them. Now, if I remember correctly, that was in the 1990s...
Moderator
 
Join Date: Oct 2006
Location: Australia
Posts: 7,748
#5   Mar 1 '07

re: What's Your Function Flavor?


I've always tended to use the $ versions, simply because I've been using them since they were the only version. Possibly also influenced by the fact that I hate variants. <Anti-variant rant removed by moderator ;)>
Reply