Runtime error 91 not raised: VB6 object variable silently auto-instantiates on member access
0 reputation · 18 Mar 2025, 17:22 UTC
Observed behavior
In a VB6 Standard EXE project, I have a class module with a default property (marked via the VB_UserMemId attribute). In form code, a variable is declared as Dim obj As MyClass and a member is accessed immediately, without any Set obj = New MyClass. Instead of raising runtime error 91 ("Object variable or With block variable not set"), an instance appears to be created silently and the call succeeds.
Goal
I want a repeatable development environment where uninitialized object access fails deterministically, so initialization bugs surface during testing rather than being masked. The uncertainty is which conditions cause VB6 to auto-instantiate instead of raising error 91.
Constraints
The behavior seems to depend on whether the class exposes a default property and whether the call is early-bound or late-bound. The language documentation I have found does not clearly state when auto-instantiation supersedes error 91. Assume VB6 SP6, Option Explicit enabled.
Questions
- Under what exact conditions does VB6 auto-instantiate an object variable rather than raise error 91?
- Does late binding change this behavior compared with early-bound access?
- Is there a project-level or code-level way to make uninitialized access always raise error 91?