RDC00311 — Early binding opportunity
Severity: Information · Family: RDC (Rubberduck Core analyzers) ·
Raised by: EarlyBindingOpportunityAnalyzer
CreateObject creates an object of a class that a library the project references declares.
An object created by CreateObject is late-bound: its members are looked up by name when the code runs, so a member that is misspelled, or called with the
wrong arguments, is an error only when that line runs. The project references the library the class is of, so the object can be declared as that class and
created with New: then its members are checked when the code is compiled, and the editor knows what they are.
CreateObject is the right choice when the project does not reference the library, so that it runs on a machine that has a different version of it, or when
the object is created on another machine. Neither case raises this diagnostic.
How it is raised
| Situation | Example | Resolution |
|---|---|---|
CreateObject is given the identifier of a class that a referenced library declares, as a literal or as a constant. |
Set d = CreateObject("Scripting.Dictionary"), with a reference to Scripting |
Declare the object as the class and create it with New: Dim d As Scripting.Dictionary, then Set d = New Scripting.Dictionary. |
| The same, with an identifier that names no version of a class whose identifier names one. | CreateObject("DAO.DBEngine"), with a reference to DAO |
The same: Set e = New DAO.DBEngine. |
See also
ACCUEIL • HOME | ℹ️ BIENVENUE • WELCOME | 🧩 BÂTISSONS • BUILD | RD-VBAL | SDK | 🌐 rubberduckvba.ca