vb.net 没有可访问的“新”接受此数量的参数 - 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18252683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No accessible 'New' accepts this number of arguments - Error
提问by Syspect
I have this chunk of code
我有这段代码
Dim _timer As System.Threading.Timer
Public Sub RunTimer2()
_timer = New System.Threading.Timer(onSave(),
Nothing,
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(5))
End Sub
and I get error Overload resolution failed because no accessible 'New' accepts this number of arguments.for the line where I am trying to create the timer. Although I can see in the documentation in msdn and in the libraries that it has those 4 possible types parameters that I use. I don't get it...
我得到错误重载解析失败,因为没有可访问的“新”接受这个数量的参数。对于我尝试创建计时器的行。虽然我可以在 msdn 的文档和库中看到它有我使用的 4 种可能的类型参数。我不明白...
回答by Steven Doggart
You are not passing a delegate to the onSavefunction. You are calling the onSavefunction and passing it's return value to the Timerconstructor. You need to create the delegate to the function and pass that, like this:
您没有将委托传递给onSave函数。您正在调用该onSave函数并将其返回值传递给Timer构造函数。您需要为函数创建委托并将其传递,如下所示:
Dim _timer As System.Threading.Timer
Public Sub RunTimer2()
_timer = New System.Threading.Timer(New TimerCallback(AddressOf onSave),
Nothing,
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(5))
End Sub
Or, VB will automatically figure out the delegate type for you if you just do this:
或者,如果您这样做,VB 将自动为您找出委托类型:
Dim _timer As System.Threading.Timer
Public Sub RunTimer2()
_timer = New System.Threading.Timer(AddressOf onSave,
Nothing,
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(5))
End Sub
回答by Dee
To declare a timer in vb.net you can:
要在 vb.net 中声明计时器,您可以:
Private MyTimer As System.Threading.Timer
MyTimer = New System.Threading.Timer(AddressOf MyTimer_Tick, Nothing, RunEveryNMinutes * 60000, RunEveryNMinutes * 60000)
Private Sub MyTimer_Tick(ByVal state As Object)
WriteEventLog("Timertick")
End Sub
回答by David Sdot
That starts the timer after 5 seconds and calls it every 5 seconds
5 秒后启动计时器并每 5 秒调用一次
Private timer As System.Threading.Timer = New System.Threading.Timer(AddressOf DoWhatever, Nothing, New TimeSpan(0, 0, 5, 0), New TimeSpan(0, 0, 5, 0))
Private Sub dowhatever(sender As Object, e As Timers.ElapsedEventArgs)
' Do stuff
End Sub

