Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 在visual basic 2010中使用调用的函数调用计时器?_Vb.net_Visual Studio 2010_Function_Timer - Fatal编程技术网

Vb.net 在visual basic 2010中使用调用的函数调用计时器?

Vb.net 在visual basic 2010中使用调用的函数调用计时器?,vb.net,visual-studio-2010,function,timer,Vb.net,Visual Studio 2010,Function,Timer,我想知道是否有办法通过函数调用计时器。此函数本身通过自动计时器调用,该计时器在窗体加载时激活 Function callTimer(ByRef var1 As Integer, ByVal var2 As Integer) 'statements() Media.SystemSounds.Beep.Play() 'to ensure that it is called Call otherFunction(ByRef var3 As Integer) <-- how

我想知道是否有办法通过函数调用计时器。此函数本身通过自动计时器调用,该计时器在窗体加载时激活

Function callTimer(ByRef var1 As Integer, ByVal var2 As Integer)
    'statements()
    Media.SystemSounds.Beep.Play() 'to ensure that it is called
    Call otherFunction(ByRef var3 As Integer) <-- how do you do this??
    Return var1
End Function
函数调用计时器(ByRef var1为整数,ByVal var2为整数)
"声明()
Media.SystemSounds.Beep.Play()'以确保调用它

调用otherFunction(ByRef var3作为整数)问题在于您正在定义方法参数,而不是设置它们

您需要传递一个值,例如:

Call otherFunction(var2) 

@competable_tech的意思是,您在函数中定义函数,这是非法的。您想调用函数,而不是定义它。这方面的一个例子如下:

Shared _timer As Timer 'this is the timer control

Function callTimer(ByRef var1 As Integer, ByVal var2 As Integer)
    Media.SystemSounds.Beep.Play() 'to ensure that it is called
    Dim testInt As Integer
    testInt = 4
    Call otherFunction(testInt) '<-- this calls otherFunction and passes testInt
    Return var1
End Function

Function otherFunction(ByRef var3 As Integer)
    StartTimer(var3) ' this will start the timer and set when the timer ticks in ms
                ' (e.g. 1000ms = 1 second)
End Function

'this starts the timer and adds a handler. The handler gets called every time 
'the timer ticks
Shared Sub StartTimer(ByVal tickTimeInMilliseconds As Integer)
    _timer = New Timer(tickTimeInMilliseconds)
    AddHandler _timer.Elapsed, New ElapsedEventHandler(AddressOf Handler)
    _timer.Enabled = True
End Sub
'this is the handler that gets called every time the timer ticks
Shared Sub Handler(ByVal sender As Object, ByVal e As ElapsedEventArgs)
    ' . . . your custom code here to be called every time the timer tickets
End Sub
结果:

测试=1

测试=10

可以看到这个ByRef/ByVal示例。如果你仍然感到困惑,那就值得深入研究


编辑:此编辑包含有关的信息。

您介意分享错误是什么吗?虽然有一些错误,但它们是非常明显的错误(使用私人Sub中的变量之类的东西)解释您:我仍然不理解您的问题。你介意再解释一下吗?这让人困惑,抱歉:-(如果可能的话,我想知道如何通过函数调用计时器,而不是在窗体加载时自动加载计时器。我在上面创建了一个示例函数和
调用其他函数(ByRef var3 As Integer
是我想要更正的。函数callTimer本身是一个由计时器调用的函数(原始问题中未显示)。它基本上类似于:onLoad timer调用callTimer(上面的函数)调用另一个名为otherFunction的计时器,然后运行某个任务。那么
otherFunction
到底做什么呢?哦,是的,对不起,我没有用它作为我的函数名,所以我忘了。所以我不需要所有的“byref”东西?只是var2?@ChaseYuan我想他说的是你在一个方法中定义一个新方法,而不是调用一个方法。看看这个链接,也许会有帮助。@Sam我还是不明白这个问题。你介意解释一下吗?这很混乱,抱歉。等等,我想我们只能用ByVal来表示func因为它们返回值而导致的异常?感谢您的链接。
otherFunction()是否
是否具有调用计时器的功能?@ChaseYuan该代码已被修改,以包含计时器控件使用的示例。此示例已从上面的参考链接中修改。我建议您更多地了解计时器的工作方式。此外,如果您还没有读过ByVal,我建议您多读ByRef。
Sub Main()
    Dim value As Integer = 1

    ' The integer value doesn't change here when passed ByVal.
    Example1(value)
    Console.WriteLine(value)

    ' The integer value DOES change when passed ByRef.
    Example2(value)
    Console.WriteLine(value)
End Sub

'any Integer variable passed through here will leave unchanged
Sub Example1(ByVal test As Integer)
    test = 10
End Sub

'any Integer variable passed through here will leave with a value of 10
Sub Example2(ByRef test As Integer)
    test = 10
End Sub