以下是我的情景。我需要使用List.Foreach(addressof fn)
调用。但似乎我们需要将对象作为列表类型和函数fn(Type of List as Arg)
传递。我们可以有另一个参数吗?。
员工类
Class Employee
public string Name
public String DisplayName
End Class
-
Dim lstEmployee As New List(Of Employee)
Sub Main()
LoadEmployee()
PrintNames()
Update(5)
End Sub
Shared Sub PrintNames()
names.ForEach(AddressOf Print) ' This will be working fine.
End Sub
Shared Sub Update(Byval int as integer)
names.ForEach(AddressOf UpdateEmpName) ' Not possible
End Sub
Shared Sub LoadNames()
lstEmployee.Add(new Employee with{.Name="Bruce"})
lstEmployee.Add(new Employee with{.Name="Alfred"})
lstEmployee.Add(new Employee with{.Name="Tim"})
lstEmployee.Add(new Employee with{.Name="Richard"})
End Sub
Shared Sub Print(ByVal s As Employee)
Console.WriteLine(s.Name)
End Sub
Shared Sub UpdateEmpName(ByVal s As Employee. ByVal i as integer)
s.DisplayName= "EIN" +i+"-"+s.Name
End Sub
这可能吗? 。我正在使用.net 3.5
答案 0 :(得分:0)
我认为这是可能的,但是因为这是未经测试的代码,请尝试一下:
Shared Sub Update(Byval int as integer)
names.ForEach(Sub(emp)
UpdateEmpName(emp, int) 'param int from method update
End Sub)
End Sub
我无法看到names
被宣布在哪里,所以假设你已经把它放在哪里呢?