Late-bound call on Nothing: NullReferenceException or COM dispatch?
0 reputation · 11 Jan 2021, 18:52 UTC
Ambiguous runtime behavior
The Visual Basic language specification does not clearly define whether a late-bound member call on a Nothing reference should immediately raise a NullReferenceException or attempt COM dispatch first. This ambiguity produces different observable outcomes depending on the host environment — .NET Framework, .NET Core/5+, or VBA — and the Option Strict setting.
Constraints and uncertainty
With Option Strict Off, the compiler permits late-bound calls on Object variables. When that variable holds Nothing, the runtime must decide between throwing a managed NullReferenceException or forwarding the call to the underlying COM layer, which may surface a different error such as a dispatch exception. The specification leaves this decision unresolved, and no compiler flag or runtime switch standardizes the behavior across platforms.
Open questions
- Which runtime path — managed null check or COM dispatch — takes precedence when a late-bound call targets
Nothing? - Does the behavior differ between .NET Framework and .NET 6+ for the same language version?
- Is there a documented host-specific contract that implementers follow, or is the variance truly unspecified?