calling a function 
Author Message
 calling a function

I have a very basic question.
Which one is faster and efficient? To call a function in
general module or to call the same function in a class
whose object is already instantiated?

(I have few general function whom I'll be calling very
extensively. Right now I kept them in a general module. I
can keep them in a class and create an object of it at the
start of the program and clear it at the end.)

Any links will be greatly appreciated. thanx alot guys...



Fri, 16 Jul 2004 23:12:30 GMT  
 calling a function


Quote:
>I have a very basic question.
>Which one is faster and efficient? To call a function in
>general module or to call the same function in a class
>whose object is already instantiated?

It is almost certainly faster to call a general BAS module.  But
unless speed is a *real big* issue for you, it is much better practice
to keep classes.

James



Sat, 17 Jul 2004 00:23:41 GMT  
 calling a function


Fri, 19 Jun 1992 00:00:00 GMT  
 calling a function
Here's a trick I learned.

In your bas mod, create a private reference to your class.
private oClass as MyClass

Expose a public function in your basmod that accepts a parameter as your
class.

Public function setMyClass(ByRef oObject as MyClass) as Boolean
    set oClass = oObject
    setMyClass = true
    'You should Err trap this and return false if it failes.
End Sub

When your class is initialized, call the above sub from your class passing
itself.
Private Sub Class_Initialize()
    bRetVal = setMyClass(Me)
    ' more tapping is a good idea here. If bRetVal is false, you should
raise an Error and terminate
End Sub

If done correctly, your basmod will hold a reference to your class.

This is REALLY handy if you need timer functionality in a class.  I believe
since AddressOf can only reference a basmod procedure, you are forced to use
SetTimer and KillTimer API's there. Then when the SetTimer fires, you call
an event in your class and proceed as normal.

Just in case someone else hits the same wall I did...

Chris



Sat, 17 Jul 2004 06:37:52 GMT  
 calling a function

Quote:

> This is REALLY handy if you need timer functionality in a class.  I believe
> since AddressOf can only reference a basmod procedure, you are forced to use
> SetTimer and KillTimer API's there. Then when the SetTimer fires, you call
> an event in your class and proceed as normal.

IIRC, the CCRP timers don't need to be hosted in a form, and they might
also work in a multi-threaded ActiveX EXE or apartment-threaded ActiveX
DLL: URL:http://mvps.org/ccrp/

Get 'em before Microshaft discovers yet another way to "encourage" us to
"upgrade" to VB.NyET!

--
Joe Foster <mailto:jlfoster%40znet.com>     On the cans? <http://www.xenu.net/>
WARNING: I cannot be held responsible for the above        They're   coming  to
because  my cats have  apparently  learned to type.        take me away, ha ha!



Sat, 17 Jul 2004 09:01:11 GMT  
 calling a function
Joe,

Very true.

I found a sample app that had the formless timer:

 Downloaded from: http://hjem.get2net.dk/vcoders/cm

But the example calls back to a form.
Since I was doing an activeX and needed timer events, I modified it in the
following way.

************************  CODE IN BASMOD ****************************

Public Declare Function SetTimer Lib "user32" _
       (ByVal hwnd As Long, ByVal nIDEvent As Long, _
       ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long

Public Declare Function KillTimer Lib "user32" _
       (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long

Private oMyObj As MyClass
Private HwndTimer As Long

Public Sub TimerProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal idEvent
As Long, ByVal dwTimer As Long)

        oMyObj.TimerEvent

End Sub

Public Sub StartTimer(Optional mstime = 1500)

    HwndTimer = SetTimer(&H0, &H0, mstime, AddressOf TimerProc)

End Sub

Public Sub StopTimer()

    If HwndTimer > 0 Then
        KillTimer &H0, HwndTimer
    End If

End Sub

Public Function SetMyObj(ByRef oPassed As MyClass) As Boolean

    Set oMyObj = oPassed
    SetMyObj= True

End Function

************************  END CODE IN BASMOD ************************

************************  CODE IN CLASS ************************
Then in my class, I do this:

Private Sub Class_Initialize()

    bRetVal = SetMyObj(Me)

End Sub

Public Sub EnableAutoProcess(Optional ByVal msWait As Integer = 1500)

    ' launch timer
    StartTimer msWait

End Sub

Public Sub KillAutoProcess()

        StopTimer

End Sub

Public Sub TimerEvent()

    'Execute my timer dependent logic here

End Sub

Private Sub Class_Terminate()

    'Clean up the timer, or else!!!

End Sub

************************  END CODE IN CLASS ************************

This example is really simple, and there are a not of holes in it. The
StartTimer and StopTimer should be functions and return a boolean on success
or failure.
Then the class should clean up the timer stuff before it unloads.
If you don't, it's an easy way to GPF vb.. :)

Anyway, I've had great luck with this approach, and I don't have to have any
other DLL's or OCX's in my project.
As well, I can create multiple functions in the BasMod for various timer
dependant logic for different classes.

Perhaps it's a little kludgy, but it works for me!

Chris




Quote:

> > This is REALLY handy if you need timer functionality in a class.  I
believe
> > since AddressOf can only reference a basmod procedure, you are forced to
use
> > SetTimer and KillTimer API's there. Then when the SetTimer fires, you
call
> > an event in your class and proceed as normal.

> IIRC, the CCRP timers don't need to be hosted in a form, and they might
> also work in a multi-threaded ActiveX EXE or apartment-threaded ActiveX
> DLL: URL:http://mvps.org/ccrp/

> Get 'em before Microshaft discovers yet another way to "encourage" us to
> "upgrade" to VB.NyET!

> --
> Joe Foster <mailto:jlfoster%40znet.com>     On the cans?

<http://www.xenu.net/>

- Show quoted text -

Quote:
> WARNING: I cannot be held responsible for the above        They're
coming  to
> because  my cats have  apparently  learned to type.        take me away,
ha ha!



Sat, 17 Jul 2004 23:59:37 GMT  
 calling a function
On another note.. the TimerEvent sub in the class should be Friend instead
of Public if you don't need to invoke this procedure externally.

Chris



Quote:
> Joe,

> Very true.

> I found a sample app that had the formless timer:

>  Downloaded from: http://hjem.get2net.dk/vcoders/cm

> But the example calls back to a form.
> Since I was doing an activeX and needed timer events, I modified it in the
> following way.

> ************************  CODE IN BASMOD ****************************

> Public Declare Function SetTimer Lib "user32" _
>        (ByVal hwnd As Long, ByVal nIDEvent As Long, _
>        ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long

> Public Declare Function KillTimer Lib "user32" _
>        (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long

> Private oMyObj As MyClass
> Private HwndTimer As Long

> Public Sub TimerProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal idEvent
> As Long, ByVal dwTimer As Long)

>         oMyObj.TimerEvent

> End Sub

> Public Sub StartTimer(Optional mstime = 1500)

>     HwndTimer = SetTimer(&H0, &H0, mstime, AddressOf TimerProc)

> End Sub

> Public Sub StopTimer()

>     If HwndTimer > 0 Then
>         KillTimer &H0, HwndTimer
>     End If

> End Sub

> Public Function SetMyObj(ByRef oPassed As MyClass) As Boolean

>     Set oMyObj = oPassed
>     SetMyObj= True

> End Function

> ************************  END CODE IN BASMOD ************************

> ************************  CODE IN CLASS ************************
> Then in my class, I do this:

> Private Sub Class_Initialize()

>     bRetVal = SetMyObj(Me)

> End Sub

> Public Sub EnableAutoProcess(Optional ByVal msWait As Integer = 1500)

>     ' launch timer
>     StartTimer msWait

> End Sub

> Public Sub KillAutoProcess()

>         StopTimer

> End Sub

> Public Sub TimerEvent()

>     'Execute my timer dependent logic here

> End Sub

> Private Sub Class_Terminate()

>     'Clean up the timer, or else!!!

> End Sub

> ************************  END CODE IN CLASS ************************

> This example is really simple, and there are a not of holes in it. The
> StartTimer and StopTimer should be functions and return a boolean on
success
> or failure.
> Then the class should clean up the timer stuff before it unloads.
> If you don't, it's an easy way to GPF vb.. :)

> Anyway, I've had great luck with this approach, and I don't have to have
any
> other DLL's or OCX's in my project.
> As well, I can create multiple functions in the BasMod for various timer
> dependant logic for different classes.

> Perhaps it's a little kludgy, but it works for me!

> Chris




in

> > > This is REALLY handy if you need timer functionality in a class.  I
> believe
> > > since AddressOf can only reference a basmod procedure, you are forced
to
> use
> > > SetTimer and KillTimer API's there. Then when the SetTimer fires, you
> call
> > > an event in your class and proceed as normal.

> > IIRC, the CCRP timers don't need to be hosted in a form, and they might
> > also work in a multi-threaded ActiveX EXE or apartment-threaded ActiveX
> > DLL: URL:http://mvps.org/ccrp/

> > Get 'em before Microshaft discovers yet another way to "encourage" us to
> > "upgrade" to VB.NyET!

> > --
> > Joe Foster <mailto:jlfoster%40znet.com>     On the cans?
> <http://www.xenu.net/>
> > WARNING: I cannot be held responsible for the above        They're
> coming  to
> > because  my cats have  apparently  learned to type.        take me away,
> ha ha!



Sun, 18 Jul 2004 06:40:59 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. functions calling other functions

2. eval function? how to call dynamic function name

3. Get the name of a function that calls another function

4. Setting a call back function to a function inside a control

5. Calling a Function using an array or variable

6. calling a function

7. trouble calling a function in a module

8. Call a function

9. Calling module functions from SQL through ADO

10. Call Excell function

11. Calling a function from an other form

12. Calling 2 Functions

 

 
Powered by phpBB® Forum Software