dbTalk Databases Forums  

Class module events not firing (it seems).....

comp.databases.ms-access comp.databases.ms-access


Discuss Class module events not firing (it seems)..... in the comp.databases.ms-access forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
The Frog
 
Posts: n/a

Default Class module events not firing (it seems)..... - 04-01-2010 , 04:16 AM






Hi Everyone,

I have built a class module that fires some events given specific
circumstances. When I step through the code the RaiseEvent EventName
is called, and the event is declared in the class as Public
EventName(). Pretty standard stuff it would seem.

I am tring to build a wrapper class (actually a form) around this. The
form is (in theory) going to display feedaback from the class as it
does its work while also allowing control of the class. I started with
a simple blank form, eliminated the pivot view and other views leaving
only form view, set the border style to thin, removed the control box,
and thats about it (set the caption too). Saved this simple form and
gave it a sensible name. No controls on the form at this time.

In the forms declarations section I declare a variable for my class
as:
Private WithEvents VariableName as ClassName

In the forms OnLoad event I have this:
Set VariableName = new ClassName

When I step through the code I can see that the class does in fact get
initialised. Cool I think to myself.

I select the VariableName object in the drop down, and select the
'Initialised' event. This event is declared as Public Event
Initialised() in the class's declarations. The RaiseEvent
Initialised() is called at the end of the class's initialisation -
this is the code I stepped through and the RaiseEvent line of code is
being called without error. In the forms VariableName_Initialised
event code I have a simple debug.print "Started". I never see the
output from this in the immediate window. As I step through the code
the event code on the form for this event is never reached.

Have I done something wrong here? I can post the code for the class if
necessary (a bit long), and all I have on the form is what is above.
As I understand it these events should be firing when called, but
arent. Am I missing something?

Any help appreciated

Cheers

The Frog

Reply With Quote
  #2  
Old   
The Frog
 
Posts: n/a

Default Re: Class module events not firing (it seems)..... - 04-01-2010 , 07:00 AM






OK, Ok, sorry everyone. It seems that my brain is more eager to get
things done than my common sense. It is not possible to rasie an event
during class initialisation or termination. This is of course why it
isnt doing anything :-)

Then the issue becomes - what is an effective workaround for this? I
need to create a notofication when the class has completed its
initialisation. Same with the termination of the class.

Any help appreciated.

Cheers

The Frog

Reply With Quote
  #3  
Old   
Tom van Stiphout
 
Posts: n/a

Default Re: Class module events not firing (it seems)..... - 04-01-2010 , 10:10 PM



On Thu, 1 Apr 2010 05:00:19 -0700 (PDT), The Frog
<mr.frog.to.you (AT) googlemail (DOT) com> wrote:

It seems to me after:
Set VariableName = new ClassName
the class (better: object) is created and ready to use. You could
write:
Set VariableName = new ClassName
Call Initialized()

The other thing I have done is make the constructor and destructor
extremely light-weight (essentially no-ops), and add an Init and Exit
method to do the initialization and cleanup of the object. THOSE
methods can raise events. The developer would need to know to call
these methods, or you can call them (at least Init) at each normal
method call if Init had not yet been called:
public method DoSomething
if _HasInitBeenCalled = false then
call Init()
end if
(Init would set this boolean value)

-Tom.
Microsoft Access MVP



Quote:
OK, Ok, sorry everyone. It seems that my brain is more eager to get
things done than my common sense. It is not possible to rasie an event
during class initialisation or termination. This is of course why it
isnt doing anything :-)

Then the issue becomes - what is an effective workaround for this? I
need to create a notofication when the class has completed its
initialisation. Same with the termination of the class.

Any help appreciated.

Cheers

The Frog

Reply With Quote
  #4  
Old   
Stuart McCall
 
Posts: n/a

Default Re: Class module events not firing (it seems)..... - 04-01-2010 , 11:16 PM



"Tom van Stiphout" <tom7744.no.spam (AT) cox (DOT) net> wrote

Quote:
On Thu, 1 Apr 2010 05:00:19 -0700 (PDT), The Frog
mr.frog.to.you (AT) googlemail (DOT) com> wrote:

It seems to me after:
Set VariableName = new ClassName
the class (better: object) is created and ready to use. You could
write:
Set VariableName = new ClassName
Call Initialized()

The other thing I have done is make the constructor and destructor
extremely light-weight (essentially no-ops), and add an Init and Exit
method to do the initialization and cleanup of the object. THOSE
methods can raise events. The developer would need to know to call
these methods, or you can call them (at least Init) at each normal
method call if Init had not yet been called:
public method DoSomething
if _HasInitBeenCalled = false then
call Init()
end if
(Init would set this boolean value)

-Tom.
Microsoft Access MVP
Sounds like a plan, although if I might make a suggestion, the
_HasInitBeenCalled variable could be a static var in the Init routine, the
1st line of which would check it and exit the proc if True. That way the
methods need only call the Init routine instead of doing the checking in
more than one place.

Quote:


OK, Ok, sorry everyone. It seems that my brain is more eager to get
things done than my common sense. It is not possible to rasie an event
during class initialisation or termination. This is of course why it
isnt doing anything :-)

Then the issue becomes - what is an effective workaround for this? I
need to create a notofication when the class has completed its
initialisation. Same with the termination of the class.

Any help appreciated.

Cheers

The Frog

Reply With Quote
  #5  
Old   
Tom van Stiphout
 
Posts: n/a

Default Re: Class module events not firing (it seems)..... - 04-01-2010 , 11:21 PM



On Fri, 2 Apr 2010 05:16:31 +0100, "Stuart McCall"
<smccall (AT) myunrealbox (DOT) com> wrote:

Good point!

-Tom.
Microsoft Access MVP


Quote:
"Tom van Stiphout" <tom7744.no.spam (AT) cox (DOT) net> wrote in message
news:vlnar5pbm4odn7pg0prhdis4mo7l8h0pu7 (AT) 4ax (DOT) com...
On Thu, 1 Apr 2010 05:00:19 -0700 (PDT), The Frog
mr.frog.to.you (AT) googlemail (DOT) com> wrote:

It seems to me after:
Set VariableName = new ClassName
the class (better: object) is created and ready to use. You could
write:
Set VariableName = new ClassName
Call Initialized()

The other thing I have done is make the constructor and destructor
extremely light-weight (essentially no-ops), and add an Init and Exit
method to do the initialization and cleanup of the object. THOSE
methods can raise events. The developer would need to know to call
these methods, or you can call them (at least Init) at each normal
method call if Init had not yet been called:
public method DoSomething
if _HasInitBeenCalled = false then
call Init()
end if
(Init would set this boolean value)

-Tom.
Microsoft Access MVP

Sounds like a plan, although if I might make a suggestion, the
_HasInitBeenCalled variable could be a static var in the Init routine, the
1st line of which would check it and exit the proc if True. That way the
methods need only call the Init routine instead of doing the checking in
more than one place.




OK, Ok, sorry everyone. It seems that my brain is more eager to get
things done than my common sense. It is not possible to rasie an event
during class initialisation or termination. This is of course why it
isnt doing anything :-)

Then the issue becomes - what is an effective workaround for this? I
need to create a notofication when the class has completed its
initialisation. Same with the termination of the class.

Any help appreciated.

Cheers

The Frog

Reply With Quote
  #6  
Old   
rkc
 
Posts: n/a

Default Re: Class module events not firing (it seems)..... - 04-04-2010 , 10:21 PM



I can think of a way to get a message when a class is terminated.
I think you may just have to make the assumption that it is Initiated
when you set
the object variable.

The termination solution involves another class contained by the first
class. The contained
class actually raises the event. The following code is a minimal,
functional demo.

[class1]
Option Compare Database
Option Explicit

Private n As clsNotify

Public Property Set notify(nt As clsNotify)
Set n = nt
End Property

Private Sub Class_Terminate()
n.CallNotify
End Sub
[/class1]

[clsNotify]
Option Compare Database
Option Explicit

Public Event notify(note As String)

Public Sub CallNotify()
RaiseEvent notify("Terminated")
End Sub
[/clsNotify]

[form1]
Option Compare Database
Option Explicit

Private c As class1
Private WithEvents n As clsNotify


Private Sub Command3_Click()
Set c = New class1
Set n = New clsNotify
Set c.notify = n
End Sub

Private Sub Command4_Click()
Set c = Nothing
End Sub

Private Sub n_notify(note As String)
Me.Text1.Value = note
End Sub
[/form1]

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.