PDA

Click to See Complete Forum and Search --> : Delegates?


Dillinger4
Feb 14th, 2002, 12:22 PM
So what would actually be the proper procedure when calling a method and passing a function pointer to it. like the DoSort(myPeople, AddressOf Person.CompareName) method we have.

Would we pass the address of the function or the address of the delegate? And if we do pass the address of the function what happens if the parameters don't match? Is the delegate then clalled instead? It seems like there is some dynamic binding going on under the scenes also. :)

Scott Penner
Feb 14th, 2002, 01:41 PM
I'm not sure why you would use the AddressOf operator at all, unless you are working with an API callback...

I'm not an expert, so correct me if I'm wrong...

Once you declare the delegate function, you can use it as a parameter type in the method. This is not an address to a delegate, but rather a delegate object type. This is the way .net keeps track of type safety. I don't think type safety is possible with the use of the AddressOf operator.

Dillinger4
Feb 14th, 2002, 02:32 PM
I'm not sure why you would use the AddressOf operator at all, unless you are working with an API callback...


It seems that they are using the AddressOf operator to pass a pointer address of a function to a function so that function can call another function using the pointer that was passed.

The DoSort method has a signature which takes a date object and a delegate as arguements. And invokes the GreaterThan delagate using the invoke keyword.

Public Sub DoSort(ByVal theDate as Object(), ByVal GreaterThan as Compare)

if GreaterThan.Invoke(theDate(outer), theDate(inner)) Then

But when we invoke the DoSort method from the public Sub button2_Click event we are passing the address of another method not the delegate.

DoSort(myPeople, AddressOf Person.CompareAge)

It seem like they are declaring a delegate and overloading it with another function to take a specific type rather then what the delegate declared as taking an Object type. So it seems like the
delegate is a template of sorts. I don't know it's very confusing. :confused: :p Ill have to look into this more. Thnks. :)