vb.net - pass parameter from loop value to dynamic function -
i making loop make array of timers , give each timer function
here's did:
dim timer(10) timer = 0 5 timer(i) = new timer addhandler timer(i).tick, function(senderx, ex) timerfunction(i) next
i have function:
private function timerfunction(byval timerno integer) msgbox(timerno) end function
but getting 6
value of timerno
every timer
call this:
timer(3).start()
i outputs 6
in change parameter number 1
5
why diong that?
you have "closed on loop variable". value of timerno
evaluated @ time function called after loop has completed, value of timerno
6.
you should have got compiler warning: "bc42324 using iteration variable in lambda expression may have unexpected results. instead, create local variable within loop , assign value of iteration variable.
"
to example...
dim timer(10) timer integer = 0 5 dim j integer = timer(i) = new timer timer(i).interval = 1000 timer(i).enabled = true addhandler timer(i).tick, function(senderx, ex) timerfunction(j) next function timerfunction(timerno integer) string msgbox(timerno) return timerno.tostring end function
this way, new instance of j
created each iteration of loop.
Comments
Post a Comment