5.4.3.9 Set Statement
Note
This section describes the implementation of MS-VBAL §5.4.3.9 Set Statement.
Syntax
| AST node | Instruction kind(s) | Notes |
|---|---|---|
AssignmentStatementNode (Kind: Set) |
Simple |
The same node type as Let, LSet and RSet, distinguished by Kind. |
See RD-VBAL §3.4.2 Simple Statements.
Static Semantics
The target is a variable, a property or an unbound member: MS-VBAL §5.4.3.9 makes the statement invalid when it
"is classified as something other than a variable, property or unbound member". A constant is
VBC09346 (AssignmentToConstantNotPermitted).
A function named as the target outside its own body - or a property of a module that has no Property Set - is a call of
it, as it is for a Let statement (RD-VBAL §5.4.3.8 Let Statement), and what
it returns is Set-assigned through its default member. As in MS-VBA (recorded in Excel), one that takes an argument is
VBC09339 Argument not optional - before what its type is (Set Left = Nothing,
Set Left$ = Nothing) - and one whose declared type is neither a Variant nor an object is
VBC09345 Invalid use of property (Set F = Nothing of a Function F() As Long), as is
Set Err = Nothing: MS-VBA's library declares Err a property. What passes is called when it runs, and what returns no
object is error 424 (Set Command = Nothing, Set Now = Nothing).
A property of a module of the project that has a Property Get and no Property Set is
VBC09345 Invalid use of property, unless what its Property Get returns has a default
member that can be Set. As in MS-VBA (recorded in Excel), this holds wherever the statement is written: in the property's
own module, in another module by its bare name (Set Fixed = Nothing), or through its module (Set Store.Fixed = Nothing).
Only a property of a library is called instead, as Set Now = Nothing is.
The default instance variable of a predeclared class is a valid target of a Set assignment, whatever is assigned to it,
as it is in MS-VBA.
MS-VBAL §5.2.4.1.2 Default Instance Variables Static Semantics
says it is invalid; see
RD-VBAL §5.2.4.1.2.
Statement, for a predeclared class Widget |
Result |
|---|---|
Set Widget = New Widget |
Valid. The default instance holds the new object. |
Set Widget = Nothing |
Valid. The default instance lets go of its object, and the next reference to Widget creates another. |
A Set assignment to a local variable or field that is itself named Widget |
Valid. The local variable or field hides the default instance of class Widget, and is an ordinary Set target. |
Set Sheet1 = …, the default instance of an extensible class (a document module) |
Compile error VBC09345 Invalid use of property, whatever is assigned. |
Set Debug = … |
Compile error VBC00001 Syntax error. |
See RD-VBAL §3.1.1 Attributes (VB_PredeclaredId, VB_Extensible).
Runtime Semantics
The executor dispatches a Set-assignment statement to its statement runtime semantics: Set-assignment is one of the
statement kinds the Simple statement provider handles
(RD-VBAL §3.5.4 Execution).
- The value expression is evaluated.
- The value is Set-coerced (MS-VBAL §5.5.2.2 Runtime semantics; RD-VBAL §5.5.2.2 Runtime semantics).
- The Set-coerced value is assigned to the target.
Two things happen around the assignment of an object to a variable.
- References. The variable holds a reference to the object it is given, and the object it held loses one. An object that loses its last reference is destroyed, and its
Class_Terminatehandler runs (RD-VBAL §5.3.1.10). WithEvents. When the variable is declaredWithEvents(RD-VBAL §5.2.3.1.2), its event handlers are detached from the object it holds before the assignment, and attached to the object it is given after it. They handle the events that object raises withRaiseEvent(RD-VBAL §5.4.2.20); an object that is destroyed handles no more.
A property of a standard module that has a Property Set is assigned by invoking its Property Set with the value, on no object:
by its bare name or through its module (Set Store.Items = New Collection), from any module. A property of a class, Set by its bare
name in the class's own code, is invoked on the object whose code it is.
Set obj.Member = value assigns a member as Let does (RD-VBAL §5.4.3.8), through the
Property Set or the public variable, and with the same two things around it: the references and the WithEvents handlers are those of
the object the variable belongs to.
The Set statement performs Set-coercion through the same direct entry point that
RDCore.Runtime.Semantics.Statements.WithStatementRuntimeSemantics uses for its own With-target coercion, not
through the operator pipeline. Set-coercion does not select a strategy per destination type, so it does not need
the operator pipeline. Let-assignment, by contrast, reuses the operator pipeline; see
RD-VBAL §5.4.3.8 Let Statement.
Implementation
| Type or member | Role |
|---|---|
RDCore.Runtime.Semantics.Statements.StatementRuntimeSemanticsProvider |
Dispatches an AssignmentStatementNode whose Kind is Set. |
ISetCoercionRuntimeSemantics.EvaluateSetCoercion |
The direct Set-coercion entry point, shared with WithStatementRuntimeSemantics. |
⏮️ RD-VBAL §5.4.3.8 Let Statement | ⏭️ RD-VBAL §5.4.4 Error Handling Statements