VBA Compiler Quirks (1 Viewer)

WayneRyan

AWF VIP
Local time
Today, 03:42
Joined
Nov 19, 2002
Messages
7,122
I have a question about the mechanics of the VBA compiler.

A coworker of mine meant to type:

strX = RTrim(strY)

Instead he typed:

RTrim(strX + strY)

This was accepted by the compiler and actually ran fine.
The strings were evaluated as not equal and returned an RTrim of "False".

The "False" is not assigned to anythng and the compiler is happy to
just leave the result hanging in space.

Other "erroneous" entries really are flagged as invalid:

5 + 3 * intValue
intA + intB
Sin(intA)

These get flagged as invalid.

Can anyone shed some light as to what ruleset the compiler might be using?

Thanks,
Wayne
 

MarkK

bit cruncher
Local time
Yesterday, 19:42
Joined
Mar 17, 2004
Messages
8,183
My VBA compiler doesn't mind Sin(intA).

I think the compiler doesn't see the Function call as an expression, which would otherwise be required to be assigned to a variable, passed to a function, or used in another expression. The Function call can look exactly like a call to a Sub.

I see this as an ambiguity in VBA, that it offers Sub and Function calls, but that it doesn't strictly enforce the differences between them.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 03:42
Joined
Sep 12, 2006
Messages
15,658
why would

RTrim(strX + strY)

return false?
 

WayneRyan

AWF VIP
Local time
Today, 03:42
Joined
Nov 19, 2002
Messages
7,122
gemma,

Oops, the "+" was meant to be an equals sign.

RTrim(strX = strY)

The strings aren't equal (False) and RTrim returns "False".

But the point is that the result goes nowhere ... and VBA is happy with it, even at runtime.

Mark,

It must be treating it as if calling a sub, but in normal practice the returned value is assigned properly.

I just don't see why the compiler doesn't flag it.

Some compilers are "smart" enough to detect assignments where the variable is never referenced again. This one doesn't even detect a "non-assignment".

Thanks to you both,
Wayne
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 03:42
Joined
Sep 12, 2006
Messages
15,658
RTrim(strX + strY)
RTrim(strX = strY)

the expression strx= stry is a Boolean comparison returning true or false.

therefore rtrim(strx=stry) is a perfectly legal expression. The fact that it represents an error in programming logic is nothing to do with the compiler.

most (all?) functions can be called directly as a sub, which is what is happening in this case.

functions, unlike plain subs can also return a value for subsequent use, but it is not an error to ignore or not require the return value
 

ChrisO

Registered User.
Local time
Today, 12:42
Joined
Apr 30, 2003
Messages
3,202
RTrim(strX = strY)
and
Sin(intA)

are not possible in the VBA editor, there would be a space between the procedure name and the first left parentheses.

It would be:-
Code:
    RTrim (strX = strY)
    Sin (intA)
    ' We can also do it with the Left function.
    Left (strX = strY), 1

In the above cases the parentheses are not enclosing an argument list passed to a procedure. They are forcing an evaluation of one or two variables prior to passing the result of that evaluation to the procedure.

The space between the procedure name and the first left parentheses indicates the procedure is being called as a Subroutine and not a Function. Since there is no return value associated with a Subroutine name then there is no value left ‘hanging in space’ and no error is raised.

We have to be very carful how we present code to site.
rtrim(strx=stry) is not a perfectly legal expression. It has not shifted to proper case and it does not contain the space between rtrim and the left parentheses.

So…
RTrim(strX = strY)
Can not exist in the VBA editor.
RTrim (strX = strY)
Can exist but is calling a Subroutine which does not return anything.
MsgBox RTrim(strX = strY)
Can exist but is calling RTrim as a Function and returns False.

Chris.
 

Users who are viewing this thread

Top Bottom