0

以下是我的场景。我需要调用 using List.Foreach(addressof fn)。但似乎我们需要将对象作为 list 类型和该 function 传递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

4

1 回答 1

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声明,所以假设您在其他地方处理过它?

于 2013-09-19T13:41:08.680 回答