ठीक है, यहाँ कुछ की शुरुआत दिखाता है कि वह है:
एक कक्षा बनाएं, इसे कहते, ओह, "कक्षा 1":
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
में जोड़ा जाता है ... जो एक शुरुआत की तरह लग रहा
विकल्प: एक और मॉड्यूल में, इस प्रयास करें। बहुत सी चीजें जिन्हें मैं साफ़ कर दूंगा (और शायद होगा - यह मजेदार हो गया)। गेटटेबल/लेटेबल/सेटटेबल विशेषताओं को खोजने के लिए एक अच्छा नियमित अभिव्यक्ति, पुराने "क्लोन" फ़ंक्शंस को हटाने के लिए कोड को कई छोटे कार्यों में कोडित करना (और अंत में नया एक डालना), कुछ और स्ट्रिंगबिल्डर-आईएसएच को ड्रॉ करना (डॉन ' टी खुद को दोहराएं) concatenations, उस तरह की चीजें ऊपर।
स्रोत
2008-10-20 21:42:35
ग्रेट विचार माइक, हालांकि मुझे लगता है कि क्लोन विधि को बनाए रखने के लिए मैन्युअल रूप से मेरे मामले में आसान हो सकता है। हालांकि बहुत अच्छा विचार है। –