Function Call On Left Hand Side Of Assignment Must Return

On By In 1

There's a compile error in VBA that reads as follows:

Compile error:

Function call on left-hand side of assignment must return Variant or Object.

To produce this compile error, you just need a function:

..And then a function call on the left-hand side of an assignment - for example:

If I remove the return type from the function's signature, I get a function that returns an implicit , and the compiler is satisfied - but now there's a run-time error 424 / "Object Required".

So I make the function return an actual object:

Now at run-time the error is 438 / "Object doesn't support this property or method" - obviously, that method makes no sense whatsoever.

I cannot for the life of me think of anything that would be a valid, legitimate (and warranted?) use of a function call on the LHS of an assignment.

That compile error exists for a reason, so there must be a valid use case. What is it?

vbacompiler-errors

An Lvalue is an expression that can appear on the left-hand side of an equals sign. Likewise, an Rvalue expression appears on the right-hand side of the equals sign. Therefore the assignment statement takes the following form: The most common Lvalue expression is a variable name as follows: Suppose you make x a constant. It is still considered to be an Lvalue. However, because you cannot change a constant value, it is referred to as a non-modifiable Lvalue: Is it possible to make an assignment to a function call for example: At first glance, this makes no sense—a function isn't a variable, so how can you assign 24 to it? Actually, C++ doesallow this, and it makes sense when you understand the mechanics behind how this expression is reduced. There is one rule in the C++ standard that states: "A function call is an Lvalue only if the result type is a reference." So, if the function prototype for foo is: Then the expression above is legal C++.

Here's an example of how you might apply this: suppose you want to keep track of four players' scores. First, you define an enumeration of the players' names to use as an index into an array of scores as follows:

Next, define the prototype for the function to which you wish to assign the scores: Notice that the result type of the function is a reference, so the function may be used as an Lvalue. Now, you just need to define the function as follows: The function has a static array that holds the scores and returns the score of the player you pass as an argument. You may assign and retrieve a score as follows: It's easy to see how to retrieve a score, but how does the assignment work? First, you make a refererence to Fred's score, then the assignment 18 is made to this reference. Not too difficult when you understand how the evaluation takes place and in what order.

If the function returned a pointer, 18 would be assigned to the pointer rather than to the memory address to which the pointer refers. Again, if you returned an int, 18 would never be assigned to the scoreCard array. That is why a function call is an Lvalue only if the result type is a reference. As you can see, references are not merely syntactic sugar or another way of expressing pointers, but were designed into the language by necessity to correctly hande cases such as this.

Here is the complete source code for the scores program:

Ron Stevenhaagen

 

0 comments

Leave a Reply

Your email address will not be published. Required fields are marked *