当前位置: 动力学知识库 > 问答 > 编程问答 >

vba - Visual Basic Run-time error '91' when setting new Object

问题描述:

I'm relatively new to VBA, but not to programming. I'm trying to set up a shared instance of a class, and I keep running into Run-time error 91, where it complains about the variable not being set.

'A Module which acts as the singleton loader

Option Explicit

Private instance As GroceryDatabase

Public Function sharedInstance() As GroceryDatabase

If instance Is Nothing Then

Set instance = New GroceryDatabase 'Run-time error 91 thrown here

End If

Set sharedInstance = instance

End Function

Obviously the variable isn't set yet, which is why I'm assigning it a New GroceryDatabase. I've seen a lot of answers to this question that boil down to someone attempting to use a Nothing object, but I can't really understand why this is being thrown when I try to instantiate it.

网友答案:

The error was not specifically due to the line where the object was instantiated, but instead because of an issue in its constructor where there was in fact a variable being assigned but not Set.

This was discovered by putting a breakpoint in the constructor for GroceryDatabase. For the future, I've enabled "Break in Class Modules", found in the path Tools>Options>General. Now, the issue is immediately pinpointed when pressing the "debug" button.

分享给朋友:
您可能感兴趣的文章:
随机阅读: