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

  • Follow


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
0
Reply mr.frog.to.you (503) 4/1/2010 9:16:14 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
0
Reply The 4/1/2010 12:00:19 PM


On Thu, 1 Apr 2010 05:00:19 -0700 (PDT), The Frog
<mr.frog.to.you@googlemail.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



>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
0
Reply Tom 4/2/2010 3:10:20 AM

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


0
Reply Stuart 4/2/2010 4:16:31 AM

On Fri, 2 Apr 2010 05:16:31 +0100, "Stuart McCall"
<smccall@myunrealbox.com> wrote:

Good point!

-Tom.
Microsoft Access MVP


>"Tom van Stiphout" <tom7744.no.spam@cox.net> wrote in message 
>news:vlnar5pbm4odn7pg0prhdis4mo7l8h0pu7@4ax.com...
>> On Thu, 1 Apr 2010 05:00:19 -0700 (PDT), The Frog
>> <mr.frog.to.you@googlemail.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 
>
0
Reply Tom 4/2/2010 4:21:17 AM

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]
0
Reply rkc 4/5/2010 3:21:52 AM

5 Replies
370 Views

(page loaded in 0.102 seconds)

Similiar Articles:













7/24/2012 10:33:24 AM


Reply: