VBA: how to test for object equality (whether two variables reference the same object)

前端 未结 2 1899
悲&欢浪女
悲&欢浪女 2020-12-06 05:20

What is the operator or function to test whether two variables of the same custom object type refer to the same object? I\'ve tried

If myObject = yourObject          


        
2条回答
  •  遥遥无期
    2020-12-06 05:41

    You need to serialize the objects somehow and then compare attribute by attribute values. The "is" operator is as dumb as it gets, it only matches if another object is the same instance assigned to the compared variable. I suggest using a jsonStringify library. I adapted one for my DexTools.xlam open source project https://github.com/dexterial/Dextools/tree/master/Main starting from the Parsing JSON in Excel VBA post. It has much more added features since I added quite a few other excel objects serialization/hashing options and it is made using the vba test driven development that DexTools incorporates. It is still work in progress so dont expect miracles

提交回复
热议问题