我看过这样的代码。
Public Sub Test()
Call Init(Sub(s)
s.Width = 100
End Sub)
End Sub
但是我不知道如何将动作用作设置参数。
这是我的代码:
Public Class SettingsModel
Public Property Width As Integer = 0
End Class
Public Sub Init(settings As Action(Of SettingsModel))
Dim ac As SettingsModel = Activator.CreateInstance(Of SettingsModel)
Console.WriteLine(ac.Width)
End Sub
但是在我的Console.WriteLine(ac.Width)上,我希望该值为100,但是该值为0。
知道我在做什么错吗?
答案 0 :(得分:0)
解决方案...我需要调用设置(ac)...
Public Sub Init(settings As Action(Of SettingsModel))
Dim ac As SettingsModel = Activator.CreateInstance(Of SettingsModel)
settings(ac)
Console.WriteLine(ac.Width)
End Sub