For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic > November 2004 > Timer event in a class









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Timer event in a class
Marty

2004-11-25, 8:55 pm

Hi,

I have a class in wich I want to have a timer and being able to to
events when the timer expire.

But first I can't instantiate it, what am I doing wrong?

Here is my code:

'///
Private WithEvents myTimer1 As Timer

Private Sub Class_Initialize()
myTimer1 = New Timer
End Sub
'///


Thanks you very much!

Marty
Larry Serflaten

2004-11-25, 8:55 pm


"Marty" <xmarty99@hotmail.com> wrote
>
> I have a class in wich I want to have a timer and being able to to
> events when the timer expire.
>
> But first I can't instantiate it, what am I doing wrong?
>
> Here is my code:
>
> '///
> Private WithEvents myTimer1 As Timer
>
> Private Sub Class_Initialize()
> myTimer1 = New Timer
> End Sub
> '///



Have you checked VB Help to learn how to use the Timer?
The Timer function is not an object and cannot be created
like you show. Put a timer control on a form, and use that....

LFS

Mike D Sutton

2004-11-25, 8:55 pm

> Have you checked VB Help to learn how to use the Timer?
> The Timer function is not an object and cannot be created
> like you show. Put a timer control on a form, and use that....


Sure it's an object, it's just a function too:

'*** Class1.cls
Dim WithEvents MyTimer As Timer

Public Sub SetTimer(ByRef inTimer As Timer)
Set MyTimer = inTimer ' Set the timer and it's properties
MyTimer.Interval = 100
MyTimer.Enabled = True
End Sub

Private Sub Class_Terminate()
Set MyTimer = Nothing
End Sub

Private Sub MyTimer_Timer() ' Use Timer() method
Debug.Print "Timer object tick - " & CStr(Timer())
End Sub
'***

'*** Form1.frm
Dim MyClass As Class1

Private Sub Form_Load()
Set MyClass = New Class1 ' Add new Timer object
Call MyClass.SetTimer(Me.Controls.Add("VB.Timer", "TimerObj"))
End Sub

Private Sub Form_Unload(ByRef Cancel As Integer)
Set MyClass = Nothing ' Clean up
End Sub
'***

Since the timer object requires a visual control to be hosted on, you need to create it from the form in this way which can be a
pain. Your other option is to either write your own wait method or to use an API timer, however this generally requires a callback
function inside a module which makes it a bit of a pain to use from a class but it's quite possible. Have a look at the
Multi-monitor library on my site for an example of this if you want it, the Monitors class uses a window proc callback in a module
which relays the event to the correct instance of the class.
Here's an old post with some examples of using various waiting methods including API timers:
http://groups.google.co.uk/groups?s...FTNGP11.phx.gbl
Hope this helps,

Mike


- Microsoft Visual Basic MVP -
E-Mail: EDais@mvps.org
WWW: Http://EDais.mvps.org/


Marty

2004-11-25, 8:55 pm

Hi Mike,

Of course your feedback help me, thank you very much!

Marty


Mike D Sutton wrote:

>
>
> Sure it's an object, it's just a function too:
>
> '*** Class1.cls
> Dim WithEvents MyTimer As Timer
>
> Public Sub SetTimer(ByRef inTimer As Timer)
> Set MyTimer = inTimer ' Set the timer and it's properties
> MyTimer.Interval = 100
> MyTimer.Enabled = True
> End Sub
>
> Private Sub Class_Terminate()
> Set MyTimer = Nothing
> End Sub
>
> Private Sub MyTimer_Timer() ' Use Timer() method
> Debug.Print "Timer object tick - " & CStr(Timer())
> End Sub
> '***
>
> '*** Form1.frm
> Dim MyClass As Class1
>
> Private Sub Form_Load()
> Set MyClass = New Class1 ' Add new Timer object
> Call MyClass.SetTimer(Me.Controls.Add("VB.Timer", "TimerObj"))
> End Sub
>
> Private Sub Form_Unload(ByRef Cancel As Integer)
> Set MyClass = Nothing ' Clean up
> End Sub
> '***
>
> Since the timer object requires a visual control to be hosted on, you need to create it from the form in this way which can be a
> pain. Your other option is to either write your own wait method or to use an API timer, however this generally requires a callback
> function inside a module which makes it a bit of a pain to use from a class but it's quite possible. Have a look at the
> Multi-monitor library on my site for an example of this if you want it, the Monitors class uses a window proc callback in a module
> which relays the event to the correct instance of the class.
> Here's an old post with some examples of using various waiting methods including API timers:
> http://groups.google.co.uk/groups?s...FTNGP11.phx.gbl
> Hope this helps,
>
> Mike
>
>
> - Microsoft Visual Basic MVP -
> E-Mail: EDais@mvps.org
> WWW: Http://EDais.mvps.org/
>
>

Michael C

2004-11-25, 8:55 pm

"Mike D Sutton" <EDais@mvps.org> wrote in message
news:e6Mue2y0EHA.1652@TK2MSFTNGP11.phx.gbl...
> Public Sub SetTimer(ByRef inTimer As Timer)


It's better to use ByVal passing objects.

Michael


Mike D Sutton

2004-11-26, 3:55 am

> It's better to use ByVal passing objects.

In what way 'better'? I was under the impression that the only difference with the passing mechanism on object variables is that
using ByVal you can't assign a new object instance to it (but member variables are still accessible) - In either case it still only
passes a 4-byte pointer so there is no performance difference, is there something I'm missing?
Cheers,

Mike


- Microsoft Visual Basic MVP -
E-Mail: EDais@mvps.org
WWW: Http://EDais.mvps.org/


Petrik Salovaara

2004-11-26, 3:55 am

"Mike D Sutton" <EDais@mvps.org> wrote in
news:#de90D10EHA.2040@tk2msftngp13.phx.gbl:

>
> In what way 'better'?


When passed ByVal, it is not possible to accidentally
set the object to Nothing, thus leading to fewer
RTE 91's in end app.

Of course this depends. There are many situations when
passing ByRef simplifies program logic a lot. But when
doing this, I always comment my code clearly so I remember
to be careful in the future.
Harvey Triana

2004-11-26, 3:55 am

Marty-
Search for XTIMER.CLS
....MSDN\2001JAN\1033\SAMPLES\VB98\Coffee

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Harvey Triana
Well Tracks .NET Developer (VB Hero)
(Software Engineer) ...Yet
VB Clásico: http://www.mvps.org/vexpert
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Las cosas deben ser sencillas pero no más. A.E.


"Marty" <xmarty99@hotmail.com> escribió en el mensaje
news:qWqpd.207098$9b.127402@edtnps84...
> Hi,
>
> I have a class in wich I want to have a timer and being able to to
> events when the timer expire.
>
> But first I can't instantiate it, what am I doing wrong?
>
> Here is my code:
>
> '///
> Private WithEvents myTimer1 As Timer
>
> Private Sub Class_Initialize()
> myTimer1 = New Timer
> End Sub
> '///
>
>
> Thanks you very much!
>
> Marty



Harvey Triana

2004-11-26, 3:55 am



--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Harvey Triana
Well Tracks .NET Developer (VB Hero)
(Software Engineer) ...Yet
VB Clásico: http://www.mvps.org/vexpert
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Las cosas deben ser sencillas pero no más. A.E.


"Marty" <xmarty99@hotmail.com> escribió en el mensaje
news:qWqpd.207098$9b.127402@edtnps84...
> Hi,
>
> I have a class in wich I want to have a timer and being able to to
> events when the timer expire.
>
> But first I can't instantiate it, what am I doing wrong?
>
> Here is my code:
>
> '///
> Private WithEvents myTimer1 As Timer
>
> Private Sub Class_Initialize()
> myTimer1 = New Timer
> End Sub
> '///
>
>
> Thanks you very much!
>
> Marty



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com