我尝试在第一次调用函数时将静态变量初始化为 1。如何正确地做到这一点?这是类型不匹配错误。
Static clip_success As Integer
If clip_success Is Nothing Then
clip_success = 1
End If
Run Code Online (Sandbox Code Playgroud)
任何原始值类型都将使用其默认值进行初始化。对于数值类型,该值为0; 对于字符串,即""(空字符串);对于日期,就是1899-12-30. ABoolean被初始化为False。
您的静态变量看起来非常像一个标志 - 应该是一个Boolean.
AVariant用特殊值初始化Empty。
任何对象引用都使用Nothing/ 空引用进行初始化。
所以:
Static clip_success As Long
If clip_success = 0 Then
clip_success = 1
End If
Run Code Online (Sandbox Code Playgroud)
或者
Static clip_success As Date
If clip_success = CDate(0) Then
clip_success = DateTime.Now
End If
Run Code Online (Sandbox Code Playgroud)
或者
Static clip_success As String
If clip_success = vbNullString Then
clip_success = "success!"
End If
Run Code Online (Sandbox Code Playgroud)
或者
Static clip_success As Variant
If IsEmpty(clip_success) Then
clip_success = 1
End If
Run Code Online (Sandbox Code Playgroud)
或者
Static clip_success As Object
If clip_success Is Nothing Then
Set clip_success = New [some class]
End If
Run Code Online (Sandbox Code Playgroud)