使用非默认值在 VBA 中初始化静态变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26424256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Initializing a static variable in VBA with non-default value
提问by Kai
Static variables in VBA are simple enough:
VBA 中的静态变量很简单:
Public Sub foo()
Static i As Integer
i = i + 1
Debug.Print i
End Sub
outputs (when called multiple times):
输出(多次调用时):
1
2
3
...
The problem is, VBA does not support initializing a variable on the same line as the declaration (not counting using :
to put two lines on one):
问题是,VBA 不支持在与声明相同的行上初始化变量(不算使用:
将两行放在一行上):
Public Sub foo()
Dim i As Integer = 5 'won't compile!
Dim j As Integer
j = 5 'we have to do this instead
End Sub
This clashes with static variables:
这与静态变量冲突:
Public Sub foo()
Static i As Integer 'we can't put an initial value here...
i = 5 'so this is how we'd usually initialize it, but...
i = i + 1
Debug.Print i
End Sub
You can probably see what happens - The very first thing the variable does every time foo
is called is set itself back to 5. Output:
您可能会看到发生了什么 - 每次foo
调用变量时所做的第一件事就是将自身设置回 5。输出:
6
6
6
...
How can you initialize a static variable in VBA to a value other than its default? Or is this just VBA dropping the ball?
如何将 VBA 中的静态变量初始化为默认值以外的值?或者这只是 VBA 丢球?
回答by Alex K.
One way to do this if you want to keep the static semantics and not switch to a global is to sniff the default value and then set the initial condition:
如果您想保留静态语义而不切换到全局语义,那么执行此操作的一种方法是嗅探默认值,然后设置初始条件:
Static i As Integer
if (i = 0) then i = 5
Safer alternative would perhaps be
更安全的选择可能是
Static i As Variant
if isempty(i) then i = 5
Or
或者
Public Sub foo(optional init as boolean = false)
Static i As Integer
if init then
i = 5
exit sub
endif
You could probably also create a class with a default property and use class_initialize
but that's probably a bit over-fussy.
您也可以创建一个具有默认属性的类并使用,class_initialize
但这可能有点过分。
回答by Felix Dombek
I had the same issue in VB6, where it's exactly the same, and I like the Microsoft recommendationmost:
我在 VB6 中遇到了同样的问题,它完全相同,我最喜欢Microsoft 的建议:
Sub initstatic ()
Static Dummy, V, I As Integer, S As String
' The code in the following if statement will be executed only once:
If IsEmpty(Dummy) Then
' Initialize the dummy variant, so that IsEmpty returns FALSE for
' subsequent calls.
Dummy = 0
' Initialize the other static variables.
V = "Great"
I = 7
S = "Visual Basic"
End If
Print "V="; V, "I="; I, "S="; S
' Static variables will retain their values over calls when changed.
V = 7
I = I + 7
S = Right$(S, 5)
End Sub
回答by Rammohan Thyagarajan
I solved it as follows using a static boolean to indicate if you are entering the function for the first time. This logic should work for other situation as well, i think
我使用静态布尔值按如下方式解决了它,以指示您是否是第一次输入该函数。这种逻辑也适用于其他情况,我认为
Private Sub Validate_Date(TB as MSForms.TextBox)
Static Previous_Value as Date
Static Not_First_Time as Boolean
if Not_First_Time = False Then
Previous_Value = Now
Not_First_Time = True
endif
if IsDate(TB.Value) = False then TB.Value = Previous_Value
Previous_Value = TB.Value
End sub