I'm reading Wrox's Professional VB 2005. I'm just at the beginning of delegates. It has us open a module1 and put in the following code:
Public Delegate Function Compare(ByVal v1 As Object, ByVal v2 As Object) As Boolean
Public Sub DoSort(ByVal theData() As Object, ByVal greaterThan As Compare)
Dim outer As Integer
Dim inner As Integer
Dim temp As Object
For outer = 0 To UBound(theData) - 1
For inner = outer + 1 To UBound(theData)
If greaterThan.Invoke(theData(outer), theData(inner)) Then
temp = theData(outer)
theData(outer) = theData(inner)
theData(inner) = temp
End If
Next
Next
End Sub
What I don't understand is how the delegate declaration than subsequently know that greaterThan is what we mean by greater than. I'm clueless on this and I don't want to go farther without understanding this. I also don't understand why even if greaterThan is understood as we mean it, the Invoke member is used.
Unfortunately, the book doesn't explain it, and it is 1100 pages long.
dennist685

Can't understand delegate function
randy.liden
However, rather than burn the book I'll give it to another beginning programmer whom I dislike.
Could you do me a favor and show me such a function and how to associate it with the delegate
dennist685
noremy
Public Function IsGreaterThan(ByVal v1 As Object, ByVal v2 As Object) As Boolean
End Function
dennist685
KhalidMirza
The greaterThan parameter of the DoSort Sub, is a reference (a pointer) to a method that is declared like the Delegate Compare (ie. it has the same paramters and return value).
What this comes down to, is that there is a function somehere that is declared somewhat like this:
Public
Function IsGreaterThan(ByVal v1 As Object, ByVal v2 As Object) As BooleanElse
End If
End Function
now the method DoSort will be called like this
Dim Range As Object() = New Object(3) {20, 12, 33, 4}
DoSort(Range, AddressOf IsGreaterThan)
The AddressOf operator creates a procedure delegate instance that points to the method.
In this case, the greaterThan parameter will 'point' to the IsGreaterThan method and this method will be executed by the greatherThan.Invoke statement.
This allows you to create multiple functions (with the same signature as the delegate) and pass them as an argument to the Sub DoSort)
If you would create a function like the one below
Public Function IsSmallerThan(ByVal v1 As Object, ByVal v2 As Object) As Boolean
End Function
You could pass a reference to that pointer to the Sub like this
Dim Range As Object() = New Object(3) {20, 12, 33, 4}
DoSort(Range, AddressOf IsSmallerThan)
This allows you to use the same method (DoSort) to sort the array descending or ascending
In this scenario, this would be confusing, since the paramter is called 'greaterThan', but as long as the parameter is a Compare delegate (i.e. a Function that accepts 2 object parameters and returns a boolean), you can pass it to the DoSort Sub
I hope this clarifies it a bit for you
ps. Oh yeah... Listen to cgraus and burn the Wrox book
Roman Benko
No idea, I don't have the book so I can't see the module you are talking about.
If the example works, it should be there somewhere though
Pete Claar
I will read msdn on delegates, thanks for the suggestion.
Could you be so kind as to show me such a function and how to associate it with the delegate
I don't think I'm going to burn the book, however. I may give it to a another beginning programmer who I dislike, however.
dennist685
NiklasEngfelt
I've never seen that syntax before. Wrox books are terrible, IMO, too many listings of code you don't need, not enough info. In this case, you'd need to create a function that matches that signature, then associate it with the delegate, so it's the function that gets called.
I would read the MSDN on delegates. And possibly burn your Wrox book :-)