5.6.8 New Expressions
Note
This section describes the implementation of MS-VBAL §5.6.8 New Expressions.
Syntax
| Expression | AST node |
|---|---|
New <class> |
NewExpressionNode |
See RD-VBAL §3.0.2 Node Types.
Static Semantics
The operand of a NewExpressionNode binds under the type binding context, through
ISymbolResolver.ResolveType. See
RD-VBAL §3.0.3 Binding Contexts and
RD-VBAL §2.3.1.3 Name Resolution.
The positional qualified-type-name rule holds wherever a type name appears: an As clause, an As New clause, and
the operand of New. In New A.B, the qualifier A is bound as a namespace (ISymbolResolver.ResolveQualifier),
and the last part B in the type binding context. See
RD-VBAL §3.0.3 Binding Contexts.
Whether a class named by New is creatable is not a name-lookup concern. It is checked once the name is bound.
A class whose VB_Creatable is False (the default instancing mode, Private, and Public Not Creatable) can only be
created by the modules of the project that defines it (MS-VBAL §5.2.4.1.1): New of one is a type mismatch from any
other project, and valid within its own.
A class of a library is creatable when the library's type library says so. MS-VBA compiles New of one, and of no
other (recorded in Excel):
New of |
Example | Outcome |
|---|---|---|
| An interface of a library | New Range, New Office.CommandBar |
Compile error VBC09368 Invalid use of New keyword. |
| A class its library does not say can be created | New Office.CommandBars, New VBA.Match |
VBC09368. |
| A class its library says can be created | New Collection, New RegExp |
Valid: the object is created when the expression is evaluated. |
| A class its library says can be created, and whose objects are never created | New ErrObject |
Valid, and Warning RDC11028. Evaluating it raises run-time error -2147221164 Class not registered. |
Runtime Semantics
Evaluating a New expression creates an object of the class and initializes it (MS-VBAL §5.3.1.10). Creating an
object of ErrObject fails: the VBA library says the class can be created, and nothing creates one. Evaluating
New ErrObject raises run-time error -2147221164 Class not registered, as MS-VBA does, and a variable that was
Nothing before Set e = New ErrObject is Nothing after it. A variable declared As New ErrObject raises the same error each time a
reference to it would create its object; Set e = Nothing creates nothing, and raises nothing.
What creating an object of another library's class gives when the program runs is that library's to say: an object, or the error its server reports.