@Compentient_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 つのことは、ByRefとByValの使用です。ByRef は、メモリ内のアドレスへの参照を渡します。これが意味することは、関数内の変数への変更は、関数の外でも保持されるということです。ただし、ByVal は関数の外では保持されず、関数に渡される前の値は関数呼び出し後と同じになります。その例を次に示します。
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
結果:
テスト = 1
テスト = 10
この ByRef/ByVal の例は、このリンクで見ることができます。まだ混乱している場合は、さらに調べる価値があります。
編集: この編集には、VB.NET でのタイマーの呼び出しに関する情報が含まれています。