Solution: Just put brackets around Value in the CallByName statement to force evaluation of it. Credit goes to Rory. Cheers!
I'm making a general function to set all the values of one variable in all my classes to a given value. It looks something like:
Sub MobClassSetAll(TargetData As String, Value As Variant)
For each MobClass in MobCollection
CallByName MobClass, TargetData, vbLet, Value
Next MobClass
End Sub
Where MobCollection is a collection of all my similar Mob classes.
The problem is CallByName
. Its fourth argument (Args) throws a type mismatch when Value
is initialized as a Variant
. I've tested this rigorously and even made a post about it here, but I have yet to get an answer.
So my new question is:
How can I change the value of the same variable in multiple similar classes, where the variable to be changed and the new value are passed to a function (without using CallByName
)?
Edit: Would overloading the function be a possible solution to this? Just learned about it in class for C++ today and I feel like its possible.
EDITED, to reduce needed helper classes to only one that holds all "main" class shared properties
Since you're already working with classes, you should go on with such a powerful feature
My understanding is that you want to change some properties for ALL objects of the same class, then the best solution is have to properties as Classes themselves, which will allow you to change them in one shot
Of course, as almost always with Classes, the preparatory work is a bit longer but the reward is worth the effort
So
Shared Property classes
Set down a
Class
to hold all the properties to be shared between ALL Mob Class instanceslet's name it after
MobShareds
and its code would simply be:Mob Class
have your
Mob
class exploit that shared properties object, so its code would beNow in your main code you have to
instantiate one object to hold shared properties
set each new
Mob
instance "shared properties" property to the same objectnow you're ready to change any wanted shared property across ALL
Mob
objects with one single statementA working code to test what above is the following:
Mob
classMobShareds
classMain code