如何保持公共静态variables未被分配?
我想有一个Dictionary dict
可用于工作表事件处理程序,所以我已经存储在模块GlobalVariables
如下:
Public dict As Dictionary
dict
在Workbook_Open
事件中初始化:
Private Sub workbook_open() Set dict = New Dictionary dict.Add "abc", "def" End Sub
这是Sheet1
的事件处理程序:
Private Sub worksheet_beforedoubleclick(ByVal target As Range, cancel As Boolean) If dict Is Nothing Then Debug.Print "nothing" Else Debug.Print "not nothing" End If End Sub
当我第一次打开工作簿并双击Sheet1
一个单元格时, dict
不是Nothing
。 但是,如果我在这一行上放置一个断点:
If dict Is Nothing Then
然后在断点停止后结束子,然后下一次双击并达到相同的断点时, dict
是Nothing
。 看起来,过早地结束_beforedoubleclick
子句有_beforedoubleclick
分配dict
的效果。 我怎样才能防止这种情况发生? 我想这个公共variables保持它的价值,而我正在debugging。
你不能阻止它发生。 如果在运行代码时需要分配公共variables(consts除外),则需要在运行任何代码的开始时调用初始化方法,以检查它们是否被分配,如果不是,则分配它们。