What is the purpose/point of $

G

Greg Maxey

I know the purpose of money and wish that I had more ;-).

What is the point of "$" in the following code. The code seems to work
just with or without it?

Sub Test()
With Dialogs(wdDialogFileOpen)
If .Display Then
MsgBox WordBasic.FilenameInfo$(.Name, 1) 'Why the "$" in this
line?
Else
MsgBox "No file selected"
End If
End With
End Sub
 
G

Greg Maxey

Dave,

Yes I have seen it used that way before e.g.,:

Dim myString$

but why is it used as shown in my original question? What purpose does
it serve?
 
J

Jonathan West

Greg Maxey said:
I know the purpose of money and wish that I had more ;-).

What is the point of "$" in the following code. The code seems to work
just with or without it?

Sub Test()
With Dialogs(wdDialogFileOpen)
If .Display Then
MsgBox WordBasic.FilenameInfo$(.Name, 1) 'Why the "$" in this
line?
Else
MsgBox "No file selected"
End If
End With
End Sub

Hi Greg,

Quite a number of string functions have versions with and without a $ on the
end. They work in the same way except for one thing. The version without the
$ (eg Left, Mid) returns a Variant of subtype string. The version with the $
(eg Left$, Mid$) returns a String directly (i.e. not wrapped in a Variant).
The contents in both cases are exactly the same. But if you are assigning
the result of the function to a string, it is a little quicker to use the $
version of the function and save yourself the double conversion to/from a
Variant. If you are assigning the result to a variable declared as a
Variant, you are better not using the $ version.

In most cases in Word VBA, the time savings are minimal compared to the
spent accessing the Word object model. But if you have a routine that
involves a lot of string handling, then using the best functions for the job
can produce noticeable performance benefits.
 
D

Dave Lett

Greg,

I can only say that it might be more efficient and is more readable (you
know what data type is being returned).

In regards to more efficient. That might not be the case, as the following
test showed:

Dim x As String
With Dialogs(wdDialogFileOpen)
If .Display Then
x = WordBasic.FilenameInfo$(.Name, 1)
MsgBox x
Else
MsgBox "No file selected"
End If
End With

If you place a breakpoint at MsgBox x and run the routine, you'll see that
data type of x is "String". However, if you comment Dim x as String and run
the routine again, then the data type of x is "Variant/String", so I'm not
sure that adding the $ is more efficient. So, having $ at the end of
FilenameInfo doesn't add much.

Dave
 
G

Greg Maxey

Dave,

Yes I see that. I was just currious. Somebody, I think it was
Jezebel, advised me not to use declarations like. Dim oString$, Dim
oCounter&, etc. I don't remember why. I took that snippet of code
from a Word FAQ that Jonathan West wrote. Perhaps he will be along to
explain. Thanks.
 
T

Tony Jollans

Hi Greg,

The FileNameInfo function (as many others, for example Left, Mid, etc.) can
return a String (with the $ suffix) or a Variant/String (without it). In
theory, a String variable is more efficient than a Variant but as the return
from a Function is a one-time use value, it rather depends on exactly what
happens behind the scenes. It probably never makes a noticeable difference.

Use of the $ is probably a matter of personal preference. I often use it as
I think it makes some things slightly clearer - but only if the reader
understands it, I guess <g>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top