Bonj said:
Let's say, for instance, I wanted a routine that would determine
whether the second letter of a given string was "a". Should I write:
Function IsSecondLetterA(InString)
IsSecondLetterA = (Mid$(InString, 2, 1) = "a")
End Function
??
That function doesn't work in VBScript, but it does in VB6.
Confusing, isn't it?
Yes this assertion is confusing, considering that this statement works fine
on my machine:
IsSecondLetterA = (Mid(InString, 2, 1) = "a")
Oh! I see the problem: you're attempting to use the "$" version of the
function. Given that all variables are variant in vbscript, the Mid$, Left$,
etc. functions are not supported. It's not explicitly mentioned on this
page:
http://msdn.microsoft.com/library/en-us/script56/html/vsgrpnonfeatures.asp
but it is implied in the Data Types row.
If I put
Mid$(InString, 2, 1) = "a"
then it changes the second letter to a whatever
You're talking about doing this in VB/VBA, right? The above statement does
not work in vbscript, because:
1. the Mid statement is not supported.
2. the "$" version of string functions is not supported
If you look up Mid in the VB/VBA documentation, you will see that it is
listed twice: once as the Mid function, and once as the Mid statement. If
you look it up in the vbscript documentation, you will only see it listed as
a function.
Just to reiterate, in this statement
x = Mid(InString, 2, 1)
Mid is a function (functions return values, statements and subs do not).
In this statement:
Mid(InString, 2, 1) = "a"
Mid is being used as a statement.
I think the other reason you are being confused by your example:
IsSecondLetterA = (Mid$(InString, 2, 1) = "a")
is because you have used the shorthand method of writing this routine. You
are actually using the Mid$ as a function, but it's hard to recognize this
because you've taken the shortcut of not assigning the value returned by
Mid$() to a variable. If you had written it out completely:
x = Mid$(InString, 2, 1)
If x = "a" then
IsSecondLetterA = True
else
IsSecondLetterA = True
end if
It would have been obvious that you were using Mid as a function. What you
need to realize is that in the background, when vb/vba/vbscript executes
this statement:
IsSecondLetterA = (Mid(InString, 2, 1) = "a")
the first thing it does is assign the value returned by the Mid function to
an implicit variable, which is used in the boolean expression, which returns
the appropriate boolean value which gets assigned to the IsSecondLetterA
variable. The implicit variable is then discarded.
Another example of this type of confusion is when people use the MsgBox
function:
if msgbox("Continue?",,"title") = vbYes then
vs
msgbox "Message",,"title"
But this situation is a little different: MsgBox is only a function and
cannot be called as a statement. Notice that when it is used to simply
display a message that no parentheses are used. If you attempt to use
parentheses in that statement you will get an error. With the Mid statement,
however, you have to use the parentheses. Try this in the VB debug window to
see:
x="abc":Mid x,2,1 = "d":?x
To me this is very confusing, since the Mid statement appears to be the only
statement that requires parentheses around its arguments. It is the only
violation of the rules I use to determine when to use parentheses:
1. If the method returns a value which is either evaluated by the calling
statement or assigned to a variable, parentheses are required.
2. Otherwise, do not use parentheses
If I wrote a subroutine which did what the Mid statement does:
Sub MyMid(ByRef pStringIn, ByVal pStartPos, _
ByVal pLength, ByVal pReplace)
pStringIn = Left(pStringIn, pStartPos -1) & _
pReplace & Mid(pStringIn, pStartPos + pLength)
End Sub
I would have to call it without parentheses:
x="abcdef":MyMid x,2,2,"hi":?x
Using parentheses would raise an error, which my rules would lead me to
expect.
Many people suggest using the Call statement to avoid this decision:
x="abcdef":Call MyMid(x,2,2,"hi"):?x
but to me, this still requires you to know when to use Call. You cannot use
Call when calling a function whose return value you wish to use, can you? I
prefer my simple rules, which have yet to let me down (except with the Mid
statment of course)
HTH,
Bob Barrows