RDC01014: Assigned ByVal parameter
Severity: Information · Family: RDC (Rubberduck Core analyzers) ·
Raised by: AssignedByValParameterAnalyzer
A procedure assigns one of its ByVal parameters.
A parameter passed by value is a copy of the argument (RD-VBAL §5.3.1.5 Parameter Lists): assigning it changes the copy, and the caller's variable keeps its value. A reader may expect the caller to see the change, or lose track of the value the procedure was given.
The value parameter of a Property Let or Property Set is passed by value even when it is declared ByRef
(RD-VBAL §5.3.1.7 Property Declarations), and is reported too.
The finding is the name of the parameter.
How it is raised
| Situation | Example | Resolution |
|---|---|---|
A ByVal parameter assigned in its procedure. |
Sub Work(ByVal count As Long) count = count + 1End Sub |
Assign a local variable of its own instead (Dim result As Long: result = count + 1), or make it ByRef if the caller is meant to see the change. |
Quick fix
The diagnostic comes with a quick fix, Copy '…' into a local variable '…', and use it in place of the parameter. At the start of the procedure it
declares a local of the parameter's type (localCount), copies the parameter into it, and writes the local wherever the procedure names the parameter.
The procedure does the same, and the parameter keeps the caller's value.
An object is copied with Set, and a Variant as what it holds. The quick fix is not offered for a procedure written on one line, for a parameter with
no As clause in a module with a DefType statement, nor for a parameter of a class or Enum type that the procedure assigns both with and without
Set, or only by passing it to another procedure.
See also
ACCUEIL • HOME | ℹ️ BIENVENUE • WELCOME | 🧩 BÂTISSONS • BUILD | RD-VBAL | SDK | 🌐 rubberduckvba.ca