Table of Contents

RDC11028: New of a class that is never created

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

The code creates an object of a class whose objects are never created, with New or with a variable declared As New. It compiles, and it fails every time it runs.

The VBA library says its ErrObject class can be created, so New ErrObject and Dim e As New ErrObject compile. Nothing creates one, though: the one ErrObject there is is the one the Err function returns. Creating another raises run-time error -2147221164 (Class not registered). That is what MS-VBA does, recorded in Excel, and RD-VBA does the same.

How it is raised

Situation Example What happens when it runs Resolution
New of the class. Set e = New ErrObject Error -2147221164. A variable that was Nothing stays Nothing. Use the error object itself: Set e = Err, or Err.Number where e.Number was.
A variable declared As New of the class, or an array of them. Dim e As New ErrObject Each statement that refers to e raises error -2147221164. Set e = Nothing raises nothing. Declare it As ErrObject, and Set e = Err.

Notes

A class its library does not say can be created at all is a compile error, as it is in MS-VBA: New Range, New Office.CommandBars, or Dim m As New VBA.Match, is VBC09368 Invalid use of New keyword.

See also


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