2008-10-20 19 views
7

Gibt es eine generische Möglichkeit, Objekte in VBA zu klonen? So dass ich x nach y kopieren könnte, anstatt nur den Zeiger zu kopieren?Klonen von Objekten in VBA?

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 

von generic meine ich so etwas wie Set y = CloneObject(x) anstatt meine eigene Methode für die Klasse kopieren seine Eigenschaften einzeln zu erstellen.

Antwort

6

OK, hier ist der Anfang von etwas, das es zeigt:

Erstellen Sie eine Klasse, nennen Sie es, oh, "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 

Jetzt müssen wir es eine Clone-Funktion geben. In einem anderen Modul, versuchen Sie dies:

Option Explicit

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 

Run, die, und das folgende in Class1 hinzugefügt wird

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

..., die wie ein Start aussieht. Viele Dinge, die ich aufräumen würde (und wahrscheinlich auch - das hat Spaß gemacht). Ein schöner Regulärer Ausdruck um getable/letable/setable Attribute zu finden, in mehrere kleine Funktionen umzuwandeln, Code um alte "Clone" Funktionen zu entfernen (und den neuen am Ende zu setzen), etwas mehr Stringbuilder-ish to DRY (Don ' t Wiederholen Sie sich selbst die Verkettungen, solche Sachen.

+0

Großartige Idee Mike, obwohl ich vermute, dass die Clone-Methode manuell beibehalten wird, könnte in meinem Fall einfacher sein. Sehr gute Idee. –

1

Ich glaube nicht, dass da etwas eingebaut ist, obwohl es nett wäre.

Ich denke, es sollte zumindest eine Möglichkeit geben, eine Clone-Methode automatisch mit dem VBA-Editor zu erstellen. Ich werde sehen, ob ich einen Blick an ihm nehmen kann, wenn ich die Kinder ins Bett haben ...

1
Private pOldinfo As YourClass 

Public Property Set clone(ByVal Value As YourClass) 
    Set pOldinfo = Value 
End Property 

das ByVal Schlüsselwort sollte Ihr Problem lösen.

Verwandte Themen