RDC01012: Variable not assigned
Severity: Warning · Family: RDC (Rubberduck Core analyzers) ·
Raised by: VariableNotAssignedAnalyzer
A variable is read, and nothing assigns it: no statement, and no procedure it is passed to.
A variable starts with the default value of its declared type: 0 for a number, "" for a String, False for a Boolean, Empty for a Variant
(RD-VBAL §5.2.3.1.4 Variable Type Declarations). A variable that is never
assigned always reads as that value, which is rarely what the code means: an assignment is often missing, or made to another name.
Only the variables that nothing outside the module can refer to are counted: a local variable, and a Private variable of the module.
A procedure assigns a variable passed to it only through a ByRef parameter that it assigns, or passes on ByRef to a procedure that does
(MS-VBAL §5.3.1.11 Procedure Invocation Argument Processing).
The procedures of the module, called by name or through Me, are followed that way, a procedure that calls itself included. A variable passed ByRef to
any other procedure is not reported: one of another module or of a library, a Declared procedure, a procedure called through an object variable, or the
handlers of an event raised with RaiseEvent.
These variables can get what they hold in other ways than an assignment, and are not reported: a dynamic array (Dim values() As Long), a variable
re-dimensioned with ReDim, a WithEvents variable, and a variable of a class or of a user-defined type. A variable declared As Object is reported: it
is Nothing until a Set assigns it, and a member access on it (rs.MoveNext) raises error 91. A fixed-size array is reported when none of its
elements is assigned, nor passed to a procedure that may assign it: each holds the default value of its type.
The finding is the name of the variable.
How it is raised
| Situation | Example | Resolution |
|---|---|---|
| A variable read and never assigned. | Dim total As LongDebug.Print total |
Assign it before it is read, or write the value it always has (Debug.Print 0). |
| A variable passed where nothing assigns it. | Dim total As LongShow total, with Sub Show(ByVal n As Long) |
Assign it before the call, or pass the value it always has. |
An Object variable that no Set assigns. |
Dim rs As Objectrs.MoveNext |
Assign it with Set before it is used. |
| A fixed-size array none of whose elements is assigned. | Dim totals(1 To 12) As CurrencyDebug.Print totals(1) |
Assign its elements before they are read. |
Quick fix
For a local variable, the diagnostic comes with a quick fix, Assign '…' its default value (…) explicitly. It writes the assignment on the line after the
declaration: total = 0 for a number, total = #12:00:00 AM# for a Date, total = vbNullString for a String, total = False for a
Boolean, total = Empty for a Variant. Nothing else assigns the variable, so it holds that value wherever it is read, and the code does what it did.
The quick fix is not offered for a variable of the module, for a fixed-length string (String * 3), whose default value is made of null characters, for an
Object variable or a fixed-size array, which no assignment of a value gives its default, nor for a declaration with no As clause or type character in a
module with a DefType statement.
See also
ACCUEIL • HOME | ℹ️ BIENVENUE • WELCOME | 🧩 BÂTISSONS • BUILD | RD-VBAL | SDK | 🌐 rubberduckvba.ca