RDC11029: Public variable of an object passed late as a copy
Severity: Hint · Family: RDC (Rubberduck Core analyzers) ·
Raised by: ObjectVariablePassedLateAnalyzer
A public variable of an object, named as a member of the object, is given to a procedure called late: a member of an Object or a Variant, whose
parameters aren't known until the program runs. VBA passes a copy of the variable. If the procedure takes it ByRef, what it assigns to the parameter
is not written back to the object.
This is a fact about how VBA passes the argument, not a problem to fix. Nothing needs to change unless the procedure is meant to change the object's variable.
So, with Public Count As Long in a class Box, t a Box with t.Count = 3, o an Object holding another Box, and
Sub Twice(ByRef n As Long): n = n * 2 in Box:
o.Twice t.Countleavest.Countat 3.o.Twice x, withxa local variable that holds 3, makesx6: a variable of the procedure is passed by reference.- The same goes when
ois aVariant, and for an element of a publicVariantof an object,o.Twice t.V(0).
This is what Microsoft Office VBA does (recorded in Excel): a public variable of a class is a property of the class's interface, and the caller passes the
value its Get returns. RD-VBA does the same. Called early, on a variable declared as its class (Dim p As Box), the same call p.Twice t.Count is
RDC11026, a warning, because the parameter is known to be ByRef.
There is no hint for a local variable, nor for a member of an object that is itself bound late (o.Twice u.Count, u an Object), which isn't known to
be a variable until the program runs.
How it is raised
| Situation | Example | What to do |
|---|---|---|
| The procedure only reads the value. | d.Exists b.Key, d an Object holding a Dictionary |
Nothing. |
| The procedure is meant to change the object's variable. | o.Twice t.Count |
Pass a local variable and assign it back: x = t.Count, o.Twice x, t.Count = x. |
See also
- RDC11026: the same copy, given to a procedure whose parameter is known to be
ByRef - RD-VBAL §5.3.1.11 Procedure Invocation Argument Processing
- RD-VBAL §2.6.4 Rubberduck Core Diagnostics
ACCUEIL • HOME | ℹ️ BIENVENUE • WELCOME | 🧩 BÂTISSONS • BUILD | RD-VBAL | SDK | 🌐 rubberduckvba.ca