【发布时间】:2014-12-15 15:30:39
【问题描述】:
我试图在我的 VB 应用程序中每小时运行一堆代码。该代码在它自己的 Sub 中工作,但是当我将它添加到此“TopOfTheHour”共享 Sub 时,我收到错误“无法在没有明确的类实例的情况下从共享方法或共享成员初始化程序中引用类的实例成员"
我在 loadlbl.Visible 中留下了一个不起作用的例子,它只是我的主窗体 (Form1) 上的一个标签控件。将刷新时间写入控制台有效,但 loadlbl.Visible = True 无效。
Private Shared Sub OnTimedEvent(source As Object, e As System.Timers.ElapsedEventArgs)
Dim aTimer As System.Timers.Timer = CType(source, System.Timers.Timer)
aTimer.Stop()
Console.WriteLine("Server Status Refreshed at " & DateTime.Now)
loadlbl.Visible = True
'Far more code is here, much of it with the same error.
aTimer.Interval = MillisecondsToNextTopOfTheHour()
aTimer.Start()
End Sub
Form 1 的所有代码(包括我想添加到此共享子程序的代码)都是 PasteBin 上的 here。 (VB语法高亮开启,代码更易阅读)
谢谢!
【问题讨论】:
-
错误意味着它所说的。如果子是共享/静态的,则不能引用仅在运行时存在的对象,包括(显然)
loadLbl之类的标签。移除 Shared 修饰符。