【问题标题】:Cloning Objects in VBA?在 VBA 中克隆对象?
【发布时间】:2010-09-18 03:15:57
【问题描述】:

有没有通用的方法在 VBA 中克隆对象?这样我就可以将 x 复制到 y 而不是仅复制指针?

  Dim x As New Class1
  Dim y As Class1

  x.Color = 1
  x.Height = 1

  Set y = x
  y.Color = 2

  Debug.Print "x.Color=" & x.Color & ", x.Height=" & x.Height

我所说的泛型是指Set y = CloneObject(x) 之类的东西,而不必为类创建自己的方法来逐一复制其属性。

【问题讨论】:

    标签: vba clone


    【解决方案1】:

    我不认为有任何内置的东西,虽然它会很好。

    我认为至少应该有一种方法可以使用 VBA 编辑器自动创建克隆方法。等我让孩子们上床睡觉后,我会看看能不能看看……

    【讨论】:

      【解决方案2】:

      好的,这是说明它的开始:

      创建一个类,叫它,哦,“Class1”:

      Option Explicit
      
      Public prop1 As Long
      Private DontCloneThis As Variant
      
      Public Property Get PrivateThing()
          PrivateThing = DontCloneThis
      End Property
      
      Public Property Let PrivateThing(value)
          DontCloneThis = value
      End Property
      

      现在我们需要给它一个克隆函数。在另一个模块中,试试这个:

      选项显式

      Public Sub makeCloneable()
      
      Dim idx As Long
      Dim line As String
      Dim words As Variant
      Dim cloneproc As String
      
      ' start building the text of our new function
          cloneproc = "Public Function Clone() As Class1" & vbCrLf
          cloneproc = cloneproc & "Set Clone = New Class1" & vbCrLf
      
          ' get the code for the class and start examining it    
          With ThisWorkbook.VBProject.VBComponents("Class1").CodeModule
      
              For idx = 1 To .CountOfLines
      
                  line = Trim(.lines(idx, 1)) ' get the next line
                  If Len(line) > 0 Then
                      line = Replace(line, "(", " ") ' to make words clearly delimited by spaces
                      words = Split(line, " ") ' so we get split on a space
                      If words(0) = "Public" Then ' can't set things declared Private
                          ' several combinations of words possible
                          If words(1) = "Property" And words(2) = "Get" Then
                              cloneproc = cloneproc & "Clone." & words(3) & "=" & words(3) & vbCrLf
                          ElseIf words(1) = "Property" And words(2) = "Set" Then
                              cloneproc = cloneproc & "Set Clone." & words(3) & "=" & words(3) & vbCrLf
                          ElseIf words(1) <> "Sub" And words(1) <> "Function" And words(1) <> "Property" Then
                              cloneproc = cloneproc & "Clone." & words(1) & "=" & words(1) & vbCrLf
                          End If
                      End If
                  End If
              Next
      
              cloneproc = cloneproc & "End Function"
      
              ' put the code into the class
              .AddFromString cloneproc
      
          End With
      
      End Sub
      

      运行它,然后将以下内容添加到 Class1 中

      Public Function Clone() As Class1
      Set Clone = New Class1
      Clone.prop1 = prop1
      Clone.PrivateThing = PrivateThing
      End Function
      

      ...这看起来像是一个开始。我会清理很多东西(并且可能会 - 这很有趣)。一个很好的正则表达式,用于查找 gettable/lettable/settable 属性,重构为几个小函数,删除旧“克隆”函数的代码(并将新函数放在最后),有点 Stringbuilder-ish to DRY (Don' t Repeat Yourself) 向上串联,诸如此类。

      【讨论】:

      • 迈克的好主意,虽然我怀疑手动维护克隆方法在我的情况下可能更容易。不过是个好主意。
      【解决方案3】:

      Scott Whitlock 在另一个问题上针对此问题发布了fantastic answer

      【讨论】:

        猜你喜欢
        • 2014-03-07
        • 2011-02-04
        • 2020-11-04
        • 2011-07-18
        • 2011-06-30
        • 1970-01-01
        • 2011-11-07
        相关资源
        最近更新 更多