Handling of event raised in other class module

V

VbaNew

Hi,

I found some very interesting programming stuff that was brought about
in the following:


Public Event SalaryChange(OldAmount As Single, NewAmount As Single,
Cancel As
Boolean)
Property Let Salary(Value As Single)
Dim Cancel As Boolean
RaiseEvent SalaryChange(pSalary, Value, Cancel)
If Cancel = True Then
MsgBox "Salary Change Overruled"
Else
pSalary = Value
End If
End Property

Now, this RaiseEvent statement is interesting in that, as opposed to a
usual procedure call, the event handler procedure called doesn't have
to exist. But the trouble is this:

1) We can't declare WithEvents in the same class or we get a compile
error!
2) The statement WithEvent *must* be in a class module.

The example above would be very useful indeed as a good class
programming example, if it could be finished. Using just the Salary
property, will one of you class gurus please be so kind as to finish
the job and modify/add the proper code in the class where the above
property comes from *and* write the regular module code and event
handler in the second class module so that it all hooks up nicely and
works? (not in the ThisWorkbook or sheet code module but in a regular
class module please).

Thanks.
 
O

onedaywhen

VbaNew said:
Hi,

I found some very interesting programming stuff that was brought about
in the following:

Public Event SalaryChange(OldAmount As Single, NewAmount As Single,
Cancel As
Boolean)
Property Let Salary(Value As Single)
Dim Cancel As Boolean
RaiseEvent SalaryChange(pSalary, Value, Cancel)
If Cancel = True Then
MsgBox "Salary Change Overruled"
Else
pSalary = Value
End If
End Property

Now, this RaiseEvent statement is interesting in that, as opposed to a
usual procedure call, the event handler procedure called doesn't have
to exist. But the trouble is this:

1) We can't declare WithEvents in the same class or we get a compile
error!
2) The statement WithEvent *must* be in a class module.

The example above would be very useful indeed as a good class
programming example, if it could be finished. Using just the Salary
property, will one of you class gurus please be so kind as to finish
the job and modify/add the proper code in the class where the above
property comes from *and* write the regular module code and event
handler in the second class module so that it all hooks up nicely and
works? (not in the ThisWorkbook or sheet code module but in a regular
class module please).

Thanks.

I think you may have misunderstood what an event is for. Please give an
example of when you would want a class to raise an event to notify
itself of a change it is making.

FWIW I think you have messed up the declaration of the event: surely
the OldAmount and NewAmount arguments should be ByVal i.e. why would
you want the client code to change them?

Jamie.

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top