How do we stop a class referencing another class?
i/e
Any changes made with tmpClass also affects myClass.....Code:Dim tmpClass() as clsOrigClass
tmpClass = myClass 'Makes a Copy
So instead of a REF how do we copy byVal?
Printable View
How do we stop a class referencing another class?
i/e
Any changes made with tmpClass also affects myClass.....Code:Dim tmpClass() as clsOrigClass
tmpClass = myClass 'Makes a Copy
So instead of a REF how do we copy byVal?
Hi some1uk03,
I'll apologize in advance for being pedantic, but I think the language surrounding this stuff is important. So, I'm confused by your use of the word "class". A class is a section of code that is used to instantiate objects. In your above question, I believe you mean "object" and not "class". And, just to further clarify, once we have instantiated some object from a class, that variable will be a handle to that object. I don't know of a case where you have a variable (or handle) that directly addresses a class.
So, to further clarify, an actual object (at least in the COM architecture) needs at least one variable referencing it, or it will un-instantiate. However, we can easily have several variables referencing it. (This is a type of aliasing, but we're better off calling it multiple references.)
So, in your example above, you have an array of objects. I assume, somewhere, you will instantiate actual objects into the elements of that array. And then, you decide to make a copy of the entire array (your second line of code). And yes, that second line of code will create secondary references to each of the objects in the array.
Now, if you want a fresh instantiation of each of your objects in that tmpClass array, you will have to individually instantiate them with the New keyword (or possibly the CreateObject function). There are other ways, but they'd be even more involved.
Bottom line, I know of no way, through array assignment, to just instantiate an entirely new set of objects from the classes-of-the-objects in an object array being copied.
Maybe that'll help.
Elroy
You would need to write your own method within your class to clone itself to a new class
Code:Public Function Clone() As myClass
Set Clone = New MyClass
... set all Clone properties from this class' current properties, i.e., Clone.PropertyA = Me.PropertyA
End Function
Also, I'm noticing the title of your thread, "Class Referencing byVal not ByRef".
It's important to realize that the variable referencing an instantiated object holds an address to that object first and foremost. When passing object variables, the ByRef and ByVal refers to that four-byte address more than the actual object. For instance, we might have the following code:
Code:
Option Explicit
Private Sub Form_Load()
Dim c As Collection
Set c = New Collection
ByRefPass c
ByValPass c
End Sub
Private Sub ByRefPass(ByRef c As Collection)
' do stuff
End Sub
Private Sub ByValPass(ByVal c As Collection)
' do stuff
End Sub
In the ByRefPass, we are essentially passing the same variable. If we un-instantiate it (Set c = Nothing), we will have also un-instantiated the object in the Form_Load procedure.
Now, in the ByValPass, we are creating a new variable, but not a new object. In other words, we are creating a second reference to the same object. And this is quite similar to what your array duplication code is doing. In that ByValPass code, we can destroy the reference to our object (Set c = Nothing), but the object will still stay instantiated because the Form_Load procedure still has a separate reference to it.
Good Luck,
Elroy
EDIT1: And, I guess what's important to recognize is that, nowhere in that sample code did we create a second collection. We were always referencing the same collection.