Table of Contents

VBC09381: User-defined type may not be passed ByVal

Severity: Error  ·  Family: VBC (static semantics / abstract syntax tree)  ·  Source metadata: VBCompileErrorInfo

A parameter of a user-defined type is declared ByVal.

A record of a user-defined type (Type ... End Type) is passed by reference: the procedure is given the caller's record, and a field it assigns is assigned in the caller's. A parameter of one declared ByVal asks for a copy, which VBA does not make.

MS-VBAL sets no such rule. MS-VBA refuses the declaration when the module is compiled, whether the procedure is called or not, with this message (recorded in Excel); RD-VBA does what MS-VBA does.

This code is raised where the declarations of a module are checked, at the parameter.

How it is raised

Situation Example Resolution
A parameter of a record declared ByVal: of a Sub or a Function. Sub Show(ByVal pt As TPoint) Remove ByVal: Sub Show(pt As TPoint). To work on a copy, copy the record in the procedure: Dim copy As TPoint: copy = pt.
A parameter of a record declared ByVal of a property, the value of a Property Let too. Property Let Origin(ByVal pt As TPoint) Remove ByVal.
A parameter of a record declared ByVal of a Declare. Declare PtrSafe Sub Zero Lib "kernel32" Alias "RtlZeroMemory" (ByVal pt As TPoint, ByVal n As LongPtr) Remove ByVal: the function is given the address of the record.

An event's parameter is the exception: MS-VBA compiles Event Moved(ByVal pt As TPoint), and so does RD-VBA.

See also


ACCUEIL • HOME | ℹ️ BIENVENUE • WELCOME | 🧩 BÂTISSONS • BUILD | RD-VBAL | SDK | 🌐 rubberduckvba.ca