Question List.ForEach(Delegate)

anandkasotia

Member
Joined
Aug 30, 2010
Messages
6
Programming Experience
1-3
Here's the code I have but I'm getting syntax issues:

Public Class frmABC

Public Delegate Sub DlgReadAndPopulate(ByRef ABC1 As AutoSQLServerVehicleCriteria, ByRef XYZ1 As SortedList(Of Integer, AutoExcelRaterPremiums))

Private Sub DoTheFollowing()
Dim aList As List(Of AutoSQLServerVehicleCriteria) = New List(Of AutoSQLServerVehicleCriteria)
Dim MyGreeting As DlgReadAndPopulate
MyGreeting = AddressOf ReadAndPopulate
aList.ForEach(AddressOf MyGreeting)This is where it has a squiggly line under MyGreeting and says: 'AddressOf' Opperand must be the name of a method (without parenthesis). Not sure what I'm doing wrong here.
End Sub

Private Sub ReadAndPopulate(ByRef ABC As AutoSQLServerVehicleCriteria, ByRef XYZ As SortedList(Of Integer, AutoExcelRaterPremiums))
Some code
End Sub

End Class


I'm just looking to call ReadAndPopulate method for each item in aList. I know I can do it the traditional "For each a in aList...Next" way but wanted to learn how to do it with delegates.
 
ForEach takes a Action(Of T) delegate as parameter, ie a method with one parameter of the item type. List(T).ForEach Method (System.Collections.Generic)

John,

Thanks for your response. I am new to delegates and was learning about it all day today and did read the msdn link that you've posted above. Does that mean that the delegate that calls the method can only have 1 parameter. Basically if I have a method with 2 parameters as in my case that I cannot use delegate with a List.ForEach?

I would very much appreciate more explanation on this. Thanks again!
 
It means you must use the Action(Of T) delegate, and the method it points to must have the matching signature Sub Method(item As T), where T is the type of the items in the list.
 
It means you must use the Action(Of T) delegate, and the method it points to must have the matching signature Sub Method(item As T), where T is the type of the items in the list.

I changed the code from:
VB.NET:
aList.ForEach(AddressOf MyGreeting)

To:
VB.NET:
aList.ForEach(New Action(Of DlgReadAndPopulate) (AddressOf MyGreeting))

Still get the same error. I believe my Delegate DlgReadAndPopulate and my Sub Method ReadAndPopulate both have the same signature i.e. with 2 parameters of same type.

I apologize if I haven't yet understood what you've been trying to say but if you can give an example or point to exactly what's wrong in my code I would truly appreciate that.
 
Your list contains AutoSQLServerVehicleCriteria type items, so that is the type of items your ForEach handler must be accepting. If you think about it, the ForEach method is supposed to do some action on each item in the list.
 
Your list contains AutoSQLServerVehicleCriteria type items, so that is the type of items your ForEach handler must be accepting. If you think about it, the ForEach method is supposed to do some action on each item in the list.

So basically what you are saying is that the Parameter that the List takes should be the only parameter the delegate should have in its signature and the Sub Method should have in its signature? Is that correct?

If the above is correct, what if I had a second parameter for the delegate and the method but was not in the List?
 
It's not a should, it is a must. The method is defined and when calling it you must supply the argument it expects.
 
If the above is correct, what if I had a second parameter for the delegate and the method but was not in the List?
The ForEach method does what it does. If what it does is not what you want done then don't use it. Use a regular For Each loop instead.

As for your original code, this is wrong:
VB.NET:
MyGreeting = AddressOf ReadAndPopulate
aList.ForEach(AddressOf MyGreeting)
You only use AddressOf once. That should be either:
VB.NET:
MyGreeting = AddressOf ReadAndPopulate
aList.ForEach(MyGreeting)
or:
VB.NET:
aList.ForEach(AddressOf ReadAndPopulate)
That still requires that the method you get the address of has the appropriate signature. In this case, as JohnH has said, that signature is that it doesn;t return a value, i.e. is a Sub, and it has one ByVal parameter of the same type as the items in the List.
 
Back
Top