代码之家  ›  专栏  ›  技术社区  ›  Heinzi

VBA CVar函数什么时候真正有用?

  •  2
  • Heinzi  · 技术社区  · 6 年前

    VBA隐式地将值转换为 Variant CVar function ? 在 example given in the documentation ,呼叫 显然是多余的。

    具体的、最小的VBA代码示例 ,其中

    • 不会编译(或产生不同的输出)当(仅!) CVar(...some expression...) 被替换为 ...some expression...

    我还没有找到这样的例子,但也许其他人可以。

    2 回复  |  直到 6 年前
        1
  •  2
  •   FunThomas    6 年前

    我想不出下面这句话在现实生活中的用法,但至少它表明你 能够 需要功能:

    Sub test()
        Dim myInt As Integer
        myInt = 2
        ' The following call will throw a runtime error in testSub
        Call testSub(myInt)   
        ' That's okay
        Call testSub(CVar(myInt))
    End Sub
    
    Sub testSub(ByRef p As Variant)
        Debug.Print "P: " & VarType(p)
        p = "ABC"
    End Sub
    
        2
  •  2
  •   GSerg    6 年前

    As Any 在里面 Declare

    Private Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
      (Destination As Any, source As Any, ByVal Length As Long)
    
    Sub Test()
      Dim source As Long, dest As Long
    
      source = 42
    
      CopyMemory dest, CVar(source), 4
      MsgBox dest
    
      CopyMemory dest, source, 4
      MsgBox dest
    End Sub