Connecting Tech Pros Worldwide Forums | Help | Site Map

dynamic timer reference problem

Kris Palmer
Guest
 
Posts: n/a
#1: Nov 21 '05
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





Chris, Master of All Things Insignificant
Guest
 
Posts: n/a
#2: Nov 21 '05

re: dynamic timer reference problem


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

"Kris Palmer" <kristeenpalmer@REMOVEyahoo.com> wrote in message
news:Xns95ECA51A4F4F8kristeenpalmeratyaho@130.81.6 4.196...[color=blue]
> 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
>
>
>
>[/color]


Larry Serflaten
Guest
 
Posts: n/a
#3: Nov 21 '05

re: dynamic timer reference problem



"Kris Palmer" <kristeenpalmer@REMOVEyahoo.com> wrote
[color=blue]
> i have a requirement to dynamically create a variable quantity of timers[/color]

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


Cor Ligthert
Guest
 
Posts: n/a
#4: Nov 21 '05

re: dynamic timer reference problem


Hi Larry,

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

:-)

Cor


Closed Thread


Similar Visual Basic .NET bytes