Table of Contents

RDC11026: Public variable of an object passed ByRef as a copy

Severity: Warning  ·  Family: RDC (Rubberduck Core analyzers)  ·  Raised by: ObjectVariablePassedByRefAnalyzer

A public variable of an object, named as a member of the object, is given to a ByRef parameter. VBA passes a copy of it, so what the procedure assigns to the parameter is not written back to the object.

So, with Public Count As Long in a class Box, and Sub Bump(ByRef n As Long): n = n + 1:

  • Bump b.Count leaves b.Count as it was.
  • Bump Me.Count, in Box's own code, leaves it as it was too, and so does Bump .Count in a With b block.
  • Bump Count, in Box's own code, adds 1 to it: there, Count is the variable itself.

The same goes for an element of a public Variant of an object: with Public V As Variant holding an array, BumpV b.V(0) leaves b.V(0) as it was, and so do BumpV Me.V(0) and BumpV .V(0). BumpV V(0), in Box's own code, changes it. With a variable of any other type, b.Items(1) (Items a Collection) is what the collection's default member returns, which the specification passes as a copy too, and there is no warning.

It makes no difference what takes the argument. The warning is raised for each of these:

  • a ByRef parameter, declared or implicit, of a procedure of the workspace;
  • a ParamArray, whose elements a procedure can assign: SetFirst b.Count leaves b.Count as it was;
  • a ByRef parameter of a Declared procedure;
  • the parameters of an event: RaiseEvent Changed(Me.Count) gives its handlers a copy, where RaiseEvent Changed(Count) gives them the variable.

A procedure called late (o.Twice b.Count, with o an Object or a Variant) is given a copy too, but what it takes isn't known until the program runs. That is RDC11029, a hint.

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. The language specification says otherwise: MS-VBAL §5.6.12 classifies b.Count as a variable, and MS-VBAL §5.3.1.11 passes a variable to a ByRef parameter by reference. RD-VBA runs the code as VBA does, and this warning stands for what the specification says: the code doesn't do what the specification gives it.

A standard module's variable is passed by reference however it is named (Counter, Module1.Counter), and gets no warning. Neither does a member of an object bound late (o.Count, o an Object or a Variant), which isn't known to be a variable until the program runs; VBA passes a copy of what it returns as well. Nor does an argument given to a member of a referenced library.

How it is raised

Situation Example Resolution
The procedure is meant to change the object's variable. Bump b.Count Assign the result back: make Bump a Function and write b.Count = Bumped(b.Count), or pass the object (Bump b) and change b.Count in it.
The procedure is meant to read the value only. Report b.Count Declare the parameter ByVal: Sub Report(ByVal n As Long).
In the class's own code. Bump Me.Count, RaiseEvent Changed(Me.Count) Name the variable by itself: Bump Count, RaiseEvent Changed(Count).
An element of the object's variable. BumpV b.V(0) Take the array out, change it, and put it back: a = b.V, BumpV a(0), b.V = a.

See also


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