There's a compile error in VBA that reads as follows:
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?
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: