is it possible to compare a delegate?

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 :wink:

One function of the original class was to make sure that the original class wasn’t already subscribed to the specific event. I did this simply by just comparing the objects. If it was found then I wouldn’t 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 it’s 2 different creations of the delegate so they would not be the same.

Comparing the variant.hash doesn’t work for the same reason. Comparing the variant.objectValue doesn’t 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:

Dim v1, v2 As Variant
v1 = WeakAddressOf Foo
v2 = WeakAddressOf Foo

Dim d1, d2 As MyDelegate
d1 = v1
d2 = v2

Dim p1, p2 As Ptr
p1 = d1
p2 = d2

If p1 = p2 Then MsgBox(“Same method”)[/code]

ah very good! I tried casting them to delegates( v) and that didn’t work at all, wouldn’t even compile as you can’t cast to a delegate. But I didn’t try just assigning them to ptr values. I will try that momentarily. Great idea!

What happens if two different objects register the same method?

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 doesn’t work at the moment. I haven’t had a chance yet to experiment with Andrew’s 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 wouldn’t have believed you could do that if you couldn’t explicitly cast them that just assigning them to the variables already cast would do it but it works perfectly thank you!

Can you get the same result using CType?

It seems so:

[code] Dim v1, v2 As Variant
v1 = WeakAddressOf Foo
v2 = WeakAddressOf Foo

Dim p1, p2 As Ptr
p1 = CType(v1, MyDelegate)
p2 = CType(v2, MyDelegate)

If p1 = p2 Then MsgBox(“Same method”)

I meant instead of using a variant.

  dim d1, d2 as mydelegate
  dim p1, p2 as ptr
  d1 = weakaddressof foo
  d2 = weakaddressof foo
  p1= ctype(d1, ptr)
  p2= ctype(d2, ptr)

  if p1 = p2 then