Is there a generic way to clone objects in VBA? So that i could copy x to y instead of copying just the pointer?
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
By generic i mean something like Set y = CloneObject(x) rather than having to create my own method for the class copying its properties one by one.
OK, here’s the beginning of something that illustrates it:
Create a class, call it, oh, ‘Class1’:
Now we need to give it a Clone function. In another module, try this:
Option Explicit
Run that, and the following gets added into Class1
…which looks like a start. Lots of things I’d clean up (and probably will – this turned out to be fun). A nice Regular Expression to find gettable/lettable/settable attributes, refactoring into several small functions, code to remove old ‘Clone’ functions (and put the new one at the end), something a bit more Stringbuilder-ish to DRY (Don’t Repeat Yourself) up the concatenations, stuff like that.