VBC09382: Only user-defined types defined in public object modules can be coerced to or from a variant or passed to late-bound functions
Severity: Error · Family: VBC (static semantics / abstract syntax tree) ·
Source metadata: VBCompileErrorInfo
A record of a user-defined type is given to a Variant.
A Variant can't hold a record of a type the project declares: the record is passed to a parameter of its own type, by reference, and assigned to a variable
of its own type. A Variant parameter - declared As Variant, declared with no type, or a ParamArray - is no parameter of the record's type, whether it is
ByVal or ByRef, and a library's method or function that takes a Variant (Collection.Add, TypeName, VarType, IsEmpty) takes no record either.
Len and LenB are the exception: they take any variable, and give the length of a record. An array of records is refused a Variant the same way.
A record of a type a referenced library declares in a public class module can be held by a Variant, which is what the message says.
MS-VBA reports the same error, with the same message (recorded in Excel), where RD-VBA used to run the call or report a Type mismatch.
This code is raised where an argument is checked against its parameter, and where a value is Let-coerced to a Variant
(LetCoercionStaticSemantics).
How it is raised
| Situation | Example | Resolution |
|---|---|---|
A record given to a Variant parameter, or to one declared with no type. |
Sub Log(v As Variant) … Log pt |
Declare the parameter as the record's type: Sub Log(pt As TPoint). |
A record given to a ParamArray. |
Sub LogAll(ParamArray items()) … LogAll pt |
Pass the fields instead: LogAll pt.X, pt.Y. |
A record given to a library's method that takes a Variant. |
items.Add pt of a Collection |
Keep the records in an array of the type (Dim points() As TPoint), or in objects of a class whose fields they are. |
A record assigned to a Variant. |
Dim v As Variant … v = pt |
Declare the variable as the record's type. |
A record given to a function of the library that takes a Variant. |
Debug.Print TypeName(pt) |
The record's type is the one it is declared with; a field of it can be given instead: TypeName(pt.X). |
An array of records given to a Variant, or assigned to one. |
Dim points(2) As TPoint … Log points |
Declare the parameter or the variable as an array of the type: Sub Log(points() As TPoint). |
See also
- RD-VBAL §5.3.1.11 Procedure Invocation Argument Processing
- RD-VBAL §5.5.1.1 Let-coercion: Static Semantics
- RD-VBAL §2.6.2 Semantic Compilation Errors
ACCUEIL • HOME | ℹ️ BIENVENUE • WELCOME | 🧩 BÂTISSONS • BUILD | RD-VBAL | SDK | 🌐 rubberduckvba.ca