dynamic timer reference problem

K

Kris Palmer

hi,
can somebody explain this problem? it's driving me crazy!
i have a requirement to dynamically create a variable quantity of timers
with associated start button based on the contents of a database. if i
create system timers dynamically, i cannot reference them from the start
button click event handler. the build error is ' <timer name> not
declared>
----
however, if i declare the system timers outside runtime code, everything
works fine! ....but that doesn't do me much good since i dont' know how
many timers i'll need at design time.
gotta be syntax or something. thanks in advance. kris


---- THIS WORKS -----

Public Class Form1
Inherits System.Windows.Forms.Form
Private t As New System.Timers.Timer 'TIMER DECLARED HERE


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'create start buttons
'add button click handlers
'add range button controls
End Sub


Public Sub btnStart(ByVal sender As Object, ByVal e As System.EventArgs)
t.Enabled = True
End Sub




---- DOES NOT BUILD ----

Public Class Form1
Inherits System.Windows.Forms.Form
'timer declaration added

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
dim t As New System.Timers.Timer 'TIMER DECLARED HERE
add elapsed time handler

'create start buttons
'add button click handlers
'add range button controls
End Sub


Public Sub btnStart(ByVal sender As Object, ByVal e As System.EventArgs)
t.Enabled = True !!!!!!! CAUSES BUILD ERROR (t not declared)
End Sub
 
C

Chris, Master of All Things Insignificant

You need something to hold your timer in. So this works. You probably want
to have "t" become an arraylist if it is going to be a dynamic amount of
timer objects you want to hold. Hope it helps.

Chirs

Public Class Form1
Inherits System.Windows.Forms.Form

dim t As System.Timers.Timer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
t = New System.Timers.Timer 'TIMER DECLARED HERE
add elapsed time handler


'create start buttons
'add button click handlers
'add range button controls
End Sub


Public Sub btnStart(ByVal sender As Object, ByVal e As System.EventArgs)
t.Enabled = True
End Sub
 
L

Larry Serflaten

Kris Palmer said:
i have a requirement to dynamically create a variable quantity of timers

If you need to time an egg boiling on the stove, and toast in the toaster,
as well as a pizza in the oven, does that mean you need to use 3 clocks
to ensure you can time them all?

Of course not, you'd use one clock to time them all, and only keep
track of the interval times required. While it may be convenient to have
a couple timers running, I would think in your case, with the need of some
variable number of timers, that you devise a means to use one timer, and
several classes that all watch the clock for their own intervals to elapse....

The reasoning is that adding multiple classes use less system resources
than adding new server based timers.

Just a suggestion!
LFS
 
C

Cor Ligthert

Hi Larry,

We in Europe have not such advanced appliances as you there, we just have on
every one a clock.

:)

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Debugging Error 1
Loop not behaving as expected 13
Unload for my Form1 ? 11
Timer fires inconsistantely 5
Tutorial, what have I missed ? 6
Timer not working 1
Latebound Exception? 3
Bindingsource problem 3

Top