ursan
ursan

Reputation: 2447

Excel VBA Assignment by value or by reference?

I have two objects of the same class which have a property that's an array of doubles (Double()), and I would like to do the following:

Obj1.arr_property = Obj2.arr_property

Is the above going to assign by value or by reference? If by reference, how to make it by value instead?

Upvotes: 2

Views: 2082

Answers (2)

Sean Meyer
Sean Meyer

Reputation: 113

This will copy the values, not the reference, into Obj1.arr_property.

One important thing to keep in mind is that the destination array must be dynamic/re-sizable. This means it needs to be declared in this form,Dim arr_property() As Double, rather than something like Dim arr_property(10) As Double. Otherwise you will get a Can't assign to array error.

Upvotes: 0

tyg
tyg

Reputation: 15150

Everything that you assign with Set only copies the reference to the new variable, not the data. You do not use Set, so all values of the array are copied, not just the reference to the array.

You actually cannot use Set in this case, even if you'd want to. Set is only allowed for objects. Arrays are no objects, so they can only be assigned the way you did.

Things are a bit different if you do not use the assignment operator =, but pass a value to a function. But that is another question.

Upvotes: 3

Related Questions