I have a notification system in an app that I originally built using class interfaces. That has worked great forever but it has limitations. I wished to update the class to instead use a delegate so that I could create several different events going to different handlers. This also worked great after I wrapped my head around delegates which took some time
One function of the original class was to make sure that the original class wasnt already subscribed to the specific event. I did this simply by just comparing the objects. If it was found then I wouldnt add it again. This does not appear to work for delegates.
I am creating the delegate by the weakAddressOf command and storing it as a variant.
Just comparing the variants always returns false. 2 variants holding the same delegate, or at least the same method pointed to via the delegate. This makes sense I suppose since its 2 different creations of the delegate so they would not be the same.
Comparing the variant.hash doesnt work for the same reason. Comparing the variant.objectValue doesnt work either. What I really want to do is compare the address stored as the delegate, is that possible?
How can I compare 2 delegates to figure out if they point to the same method?
I tried a few things and got it to work by converting the Variants into Delegates and then converting the Delegates to Ptrs, and then comparing the pointer values directly:
[code]
Dim v1, v2 As Variant
v1 = WeakAddressOf Foo
v2 = WeakAddressOf Foo
ah very good! I tried casting them to delegates( v) and that didnt work at all, wouldnt even compile as you cant cast to a delegate. But I didnt try just assigning them to ptr values. I will try that momentarily. Great idea!
then they would return true if they are the same. I can live with that. I need to be able to compare them so that you can deregister! That doesnt work at the moment. I havent had a chance yet to experiment with Andrews implicit casting them to pointers yet. but if that works then problem solved.
and it looks like assigning the variants to the delegate first and then to a ptr makes it possible to compare them! Andrew, brilliant! I wouldnt have believed you could do that if you couldnt explicitly cast them that just assigning them to the variables already cast would do it but it works perfectly thank you!